吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2817|回复: 10
收起左侧

[求助] Python使用Qtdesigner设计的UI转换后跑不出来

[复制链接]
johncc 发表于 2019-8-19 00:05
想用Qtdesigner做一个类似Excel的表格,设计完了之后预览一切正常。但是转换成py文件再运行,就出BUG了。求大神解惑~~
以下是UI文件转换成的py文件代码:
[Python] 纯文本查看 复制代码
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'test_UI.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(1830, 1259)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("../Icon/cog_icon_x_128px_1077765_easyicon.net.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        Form.setWindowIcon(icon)
        self.verticalLayout_4 = QtWidgets.QVBoxLayout(Form)
        self.verticalLayout_4.setContentsMargins(40, 20, 40, 40)
        self.verticalLayout_4.setObjectName("verticalLayout_4")
        self.verticalLayout_3 = QtWidgets.QVBoxLayout()
        self.verticalLayout_3.setObjectName("verticalLayout_3")
        self.frame = QtWidgets.QFrame(Form)
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame.setObjectName("frame")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.frame)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.pushButton = QtWidgets.QPushButton(self.frame)
        self.pushButton.setObjectName("pushButton")
        self.horizontalLayout.addWidget(self.pushButton)
        self.pushButton_2 = QtWidgets.QPushButton(self.frame)
        self.pushButton_2.setObjectName("pushButton_2")
        self.horizontalLayout.addWidget(self.pushButton_2)
        spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout.addItem(spacerItem)
        self.verticalLayout_3.addWidget(self.frame)
        self.tabWidget = QtWidgets.QTabWidget(Form)
        self.tabWidget.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.tabWidget.setTabPosition(QtWidgets.QTabWidget.West)
        self.tabWidget.setObjectName("tabWidget")
        self.tab = QtWidgets.QWidget()
        self.tab.setObjectName("tab")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.tab)
        self.verticalLayout.setObjectName("verticalLayout")
        self.label = QtWidgets.QLabel(self.tab)
        self.label.setObjectName("label")
        self.verticalLayout.addWidget(self.label, 0, QtCore.Qt.AlignHCenter)
        self.tableWidget = QtWidgets.QTableWidget(self.tab)
        self.tableWidget.setTabletTracking(False)
        self.tableWidget.setAutoFillBackground(True)
        self.tableWidget.setRowCount(50)
        self.tableWidget.setColumnCount(26)
        self.tableWidget.setObjectName("tableWidget")
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(0, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(1, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(2, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(3, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(4, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(5, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(6, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(7, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(8, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(9, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(10, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(11, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(12, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(13, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(14, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(15, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(16, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(17, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(18, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(19, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(20, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(21, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(22, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(23, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(24, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(25, item)
        self.tableWidget.horizontalHeader().setDefaultSectionSize(90)
        self.tableWidget.horizontalHeader().setMinimumSectionSize(30)
        self.tableWidget.verticalHeader().setDefaultSectionSize(28)
        self.tableWidget.verticalHeader().setMinimumSectionSize(20)
        self.verticalLayout.addWidget(self.tableWidget)
        self.tabWidget.addTab(self.tab, "")
        self.tab_2 = QtWidgets.QWidget()
        self.tab_2.setObjectName("tab_2")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.tab_2)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.label_2 = QtWidgets.QLabel(self.tab_2)
        self.label_2.setObjectName("label_2")
        self.verticalLayout_2.addWidget(self.label_2, 0, QtCore.Qt.AlignHCenter)
        self.tableWidget_2 = QtWidgets.QTableWidget(self.tab_2)
        self.tableWidget_2.setTabletTracking(False)
        self.tableWidget_2.setAutoFillBackground(True)
        self.tableWidget_2.setRowCount(50)
        self.tableWidget_2.setColumnCount(26)
        self.tableWidget_2.setObjectName("tableWidget_2")
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget_2.setHorizontalHeaderItem(0, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget_2.setHorizontalHeaderItem(1, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget_2.setHorizontalHeaderItem(2, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget_2.setHorizontalHeaderItem(3, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget_2.setHorizontalHeaderItem(4, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget_2.setHorizontalHeaderItem(5, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget_2.setHorizontalHeaderItem(6, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget_2.setHorizontalHeaderItem(7, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget_2.setHorizontalHeaderItem(8, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget_2.setHorizontalHeaderItem(9, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget_2.setHorizontalHeaderItem(10, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget_2.setHorizontalHeaderItem(11, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget_2.setHorizontalHeaderItem(12, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget_2.setHorizontalHeaderItem(13, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget_2.setHorizontalHeaderItem(14, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget_2.setHorizontalHeaderItem(15, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget_2.setHorizontalHeaderItem(16, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget_2.setHorizontalHeaderItem(17, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget_2.setHorizontalHeaderItem(18, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget_2.setHorizontalHeaderItem(19, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget_2.setHorizontalHeaderItem(20, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget_2.setHorizontalHeaderItem(21, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget_2.setHorizontalHeaderItem(22, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget_2.setHorizontalHeaderItem(23, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget_2.setHorizontalHeaderItem(24, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget_2.setHorizontalHeaderItem(25, item)
        self.tableWidget_2.horizontalHeader().setDefaultSectionSize(90)
        self.tableWidget_2.horizontalHeader().setMinimumSectionSize(30)
        self.tableWidget_2.verticalHeader().setDefaultSectionSize(28)
        self.tableWidget_2.verticalHeader().setMinimumSectionSize(20)
        self.verticalLayout_2.addWidget(self.tableWidget_2)
        self.tabWidget.addTab(self.tab_2, "")
        self.verticalLayout_3.addWidget(self.tabWidget)
        self.verticalLayout_4.addLayout(self.verticalLayout_3)

        self.retranslateUi(Form)
        self.tabWidget.setCurrentIndex(0)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "test_Ui"))
        self.pushButton.setText(_translate("Form", "Save"))
        self.pushButton_2.setText(_translate("Form", "Cancel"))
        self.label.setText(_translate("Form", "<html><head/><body><p><span style=\" font-size:16pt; font-weight:600;\">Table</span></p></body></html>"))
        item = self.tableWidget.horizontalHeaderItem(0)
        item.setText(_translate("Form", "A"))
        item = self.tableWidget.horizontalHeaderItem(1)
        item.setText(_translate("Form", "B"))
        item = self.tableWidget.horizontalHeaderItem(2)
        item.setText(_translate("Form", "C"))
        item = self.tableWidget.horizontalHeaderItem(3)
        item.setText(_translate("Form", "D"))
        item = self.tableWidget.horizontalHeaderItem(4)
        item.setText(_translate("Form", "E"))
        item = self.tableWidget.horizontalHeaderItem(5)
        item.setText(_translate("Form", "F"))
        item = self.tableWidget.horizontalHeaderItem(6)
        item.setText(_translate("Form", "G"))
        item = self.tableWidget.horizontalHeaderItem(7)
        item.setText(_translate("Form", "H"))
        item = self.tableWidget.horizontalHeaderItem(8)
        item.setText(_translate("Form", "I"))
        item = self.tableWidget.horizontalHeaderItem(9)
        item.setText(_translate("Form", "J"))
        item = self.tableWidget.horizontalHeaderItem(10)
        item.setText(_translate("Form", "K"))
        item = self.tableWidget.horizontalHeaderItem(11)
        item.setText(_translate("Form", "L"))
        item = self.tableWidget.horizontalHeaderItem(12)
        item.setText(_translate("Form", "M"))
        item = self.tableWidget.horizontalHeaderItem(13)
        item.setText(_translate("Form", "N"))
        item = self.tableWidget.horizontalHeaderItem(14)
        item.setText(_translate("Form", "O"))
        item = self.tableWidget.horizontalHeaderItem(15)
        item.setText(_translate("Form", "P"))
        item = self.tableWidget.horizontalHeaderItem(16)
        item.setText(_translate("Form", "Q"))
        item = self.tableWidget.horizontalHeaderItem(17)
        item.setText(_translate("Form", "R"))
        item = self.tableWidget.horizontalHeaderItem(18)
        item.setText(_translate("Form", "S"))
        item = self.tableWidget.horizontalHeaderItem(19)
        item.setText(_translate("Form", "T"))
        item = self.tableWidget.horizontalHeaderItem(20)
        item.setText(_translate("Form", "U"))
        item = self.tableWidget.horizontalHeaderItem(21)
        item.setText(_translate("Form", "V"))
        item = self.tableWidget.horizontalHeaderItem(22)
        item.setText(_translate("Form", "W"))
        item = self.tableWidget.horizontalHeaderItem(23)
        item.setText(_translate("Form", "X"))
        item = self.tableWidget.horizontalHeaderItem(24)
        item.setText(_translate("Form", "Y"))
        item = self.tableWidget.horizontalHeaderItem(25)
        item.setText(_translate("Form", "Z"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("Form", "Tab 1"))
        self.label_2.setText(_translate("Form", "<html><head/><body><p><span style=\" font-size:16pt; font-weight:600;\">附表1</span></p></body></html>"))
        item = self.tableWidget_2.horizontalHeaderItem(0)
        item.setText(_translate("Form", "A"))
        item = self.tableWidget_2.horizontalHeaderItem(1)
        item.setText(_translate("Form", "B"))
        item = self.tableWidget_2.horizontalHeaderItem(2)
        item.setText(_translate("Form", "C"))
        item = self.tableWidget_2.horizontalHeaderItem(3)
        item.setText(_translate("Form", "D"))
        item = self.tableWidget_2.horizontalHeaderItem(4)
        item.setText(_translate("Form", "E"))
        item = self.tableWidget_2.horizontalHeaderItem(5)
        item.setText(_translate("Form", "F"))
        item = self.tableWidget_2.horizontalHeaderItem(6)
        item.setText(_translate("Form", "G"))
        item = self.tableWidget_2.horizontalHeaderItem(7)
        item.setText(_translate("Form", "H"))
        item = self.tableWidget_2.horizontalHeaderItem(8)
        item.setText(_translate("Form", "I"))
        item = self.tableWidget_2.horizontalHeaderItem(9)
        item.setText(_translate("Form", "J"))
        item = self.tableWidget_2.horizontalHeaderItem(10)
        item.setText(_translate("Form", "K"))
        item = self.tableWidget_2.horizontalHeaderItem(11)
        item.setText(_translate("Form", "L"))
        item = self.tableWidget_2.horizontalHeaderItem(12)
        item.setText(_translate("Form", "M"))
        item = self.tableWidget_2.horizontalHeaderItem(13)
        item.setText(_translate("Form", "N"))
        item = self.tableWidget_2.horizontalHeaderItem(14)
        item.setText(_translate("Form", "O"))
        item = self.tableWidget_2.horizontalHeaderItem(15)
        item.setText(_translate("Form", "P"))
        item = self.tableWidget_2.horizontalHeaderItem(16)
        item.setText(_translate("Form", "Q"))
        item = self.tableWidget_2.horizontalHeaderItem(17)
        item.setText(_translate("Form", "R"))
        item = self.tableWidget_2.horizontalHeaderItem(18)
        item.setText(_translate("Form", "S"))
        item = self.tableWidget_2.horizontalHeaderItem(19)
        item.setText(_translate("Form", "T"))
        item = self.tableWidget_2.horizontalHeaderItem(20)
        item.setText(_translate("Form", "U"))
        item = self.tableWidget_2.horizontalHeaderItem(21)
        item.setText(_translate("Form", "V"))
        item = self.tableWidget_2.horizontalHeaderItem(22)
        item.setText(_translate("Form", "W"))
        item = self.tableWidget_2.horizontalHeaderItem(23)
        item.setText(_translate("Form", "X"))
        item = self.tableWidget_2.horizontalHeaderItem(24)
        item.setText(_translate("Form", "Y"))
        item = self.tableWidget_2.horizontalHeaderItem(25)
        item.setText(_translate("Form", "Z"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("Form", "Tab 2"))

以下是用来运行的代码:
[Python] 纯文本查看 复制代码
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication, QMenu, QTableWidgetItem
from PyQt5.QtCore import QObject, Qt

from test_UI import *


class MyMainWindow(QMainWindow, Ui_Form):
    def __init__(self):
        super(MyMainWindow, self).__init__()
        self.setupUi(self)



if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = MyMainWindow()
    win.show()
    sys.exit(app.exec_())

再贴一下两张截图:

代码运行图

代码运行图

Qtdesigner里的预览图

Qtdesigner里的预览图

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

hedefu 发表于 2019-8-19 01:48
正在学习python。支持。
Vjmms 发表于 2019-8-19 08:34

[Python] 纯文本查看 复制代码
class MyMainWindow(QMainWindow, Ui_Form):
    def __init__(self):
        super(MyMainWindow, self).__init__()
        self.setupUi(self)


改成下面试试
[Python] 纯文本查看 复制代码
class MyMainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        self.show()
小坤9 发表于 2019-8-19 09:07
Mr.Chu 发表于 2019-8-19 10:27
你应该放出.ui文件,这样检查起来方便点

另外你的错误是 .ui的窗口类型是QWidget,不是QMainWindow

[Python] 纯文本查看 复制代码
from PyQt5.QtWidgets import QWidget
class MyMainWindow(QWidget, Ui_Form):
    def __init__(self):
        super(MyMainWindow, self).__init__()
        self.setupUi(self)
 楼主| johncc 发表于 2019-8-19 12:30
Mr.Chu 发表于 2019-8-19 10:27
你应该放出.ui文件,这样检查起来方便点

另外你的错误是 .ui的窗口类型是QWidget,不是QMainWindow

的确,错误是QMainWindow,修改为QWidget就跑出来了。
(以后有问题我知道放UI文件了,第一次提问这个,哈哈哈)
 楼主| johncc 发表于 2019-8-19 13:00
顺便问一下,Qtablewidget有加粗单元格边框或者换色功能吗?像Excel里那样。我看文档只看到了设置边框显示或隐藏...
Mr.Chu 发表于 2019-8-19 14:00
johncc 发表于 2019-8-19 13:00
顺便问一下,Qtablewidget有加粗单元格边框或者换色功能吗?像Excel里那样。我看文档只看到了设置边框显示 ...

[Python] 纯文本查看 复制代码
self.tableWidget.setStyleSheet('QTableWidget::item{border:5px solid green}')

可以自己百度很容易找到答案,
 楼主| johncc 发表于 2019-8-19 14:53
Mr.Chu 发表于 2019-8-19 14:00
[mw_shl_code=python,true]self.tableWidget.setStyleSheet('QTableWidget::item{border:5px solid green ...

我百度出来的都是修改背景色和单元格内容...主要是想实现像Excel那样,选择某部分单元格,给这部分单元格外围加粗,让它更加明显,以此在同一个表格中划分不同的内容,甚至可以组成一些简单图形
Mr.Chu 发表于 2019-8-19 15:22
johncc 发表于 2019-8-19 14:53
我百度出来的都是修改背景色和单元格内容...主要是想实现像Excel那样,选择某部分单元格,给这部分单元格 ...

懂你的意思了 不过这样就很麻烦了 只能自己慢慢搞了  百度不行的话多去Google吧 很多代码直接搜索Google搜出来的结果比百度好太多了
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-27 04:15

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表