我在尝试将ui和实现文件分开时遇到一个问题.
我使用QT创建器创建* .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();
TypeError:setupUi()缺少1个必需的位置参数:“ Form”
但是如果我更改self.setupUi();进入self.setupUi(self);在myUI.py中
有用.我想知道为什么必须在self.setui()中调用self
>在myUI.py中,为什么没有def setupUi(self,Form):,而不是def setupUi(self):
我知道这是因为我们将Form视为变量,并将在以后使用它.但是,为什么我们需要它?或者我们可以将其创建为self.Form而不是调用Form
谢谢!