【发布时间】:2015-01-28 11:49:00
【问题描述】:
我在尝试分离 ui 和实现文件时有一个问题。 我使用 QT creator 创建 *.ui,然后将其转换为 *.py
main.py
from PyQt5 import QtCore, QtGui, QtWidgets
from myUI import Ui_Form
import sys
class Prog(Ui_Form):
def __init__(self):
super().__init__();
def main():
Program = QtWidgets.QApplication(sys.argv);
MyProg=Prog();
MyProg.show();
Program.exec_();
if __name__=='__main__':
main();
myUI.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(QtWidgets.QWidget):
def __init__(self):
super().__init__();
self.setupUi();
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(30, 230, 93, 28))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(210, 250, 93, 28))
self.pushButton_2.setObjectName("pushButton_2")
self.textEdit = QtWidgets.QTextEdit(Form)
self.textEdit.setGeometry(QtCore.QRect(120, 80, 104, 87))
self.textEdit.setObjectName("textEdit")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "PushButton"))
self.pushButton_2.setText(_translate("Form", "PushButton"))
我的问题是
-
我收到如下错误消息。
self.setupUi(); 类型错误:setupUi() 缺少 1 个必需的位置参数:'Form'
但如果我在 myUI.py 中将 self.setupUi(); 更改为 self.setupUi(self);
它有效。我想知道为什么必须在 self.setui()
中调用 self -
在myUI.py中,为什么有def setupUi(self, Form):,而不是def setupUi(self):
我知道这是因为我们将 Form 视为一个变量,并且稍后会使用它。但是,我们为什么需要它?或者我们可以创建一个成员为 self.Form 而不是调用 Form
谢谢!
【问题讨论】:
-
转换后的ui文件使用方法请见pyuic5 documentation。
-
所以更好的办法是声明一个 ui 类成员?但在文档中,它还提供了继承自 UI 类的示例。
标签: python user-interface inheritance pyqt pyside