如何从第一张表格中拨打第二张表格? 例如,我有MainWindow.ui和Auth.ui. 如何在主窗口上单击按钮时如何显示Auth.ui? 我正在使用QtDesigner和pyuic创建表单。
MainWindow.py
class MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(323, 118)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pb_request = QtWidgets.QPushButton(self.centralwidget)
self.pb_request.setObjectName("pb_request")
MainWindow.setCentralWidget(self.centralwidget)
self.pb_request.clicked.connect(...???)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pb_request.setText(_translate("MainWindow", "Request"))
self.label.setText(_translate("MainWindow", "Enter term you want to know"))
def main():
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = MainWindow()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
答案 0 :(得分:1)
建议不要修改Qt Designer生成的类,应该做的是创建一个使用生成的视图来隐式逻辑的类。
例如,当基于模板生成视图时,它会生成类似于以下的代码:
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
[...]
def retranslateUi(self, MainWindow):
[...]
然后你必须创建实现逻辑的类:
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent=parent)
self.setupUi(self)
如果您使用基于模板的视图:
假设您按照我的建议创建了类:
class Ui_Auth(object):
def setupUi(self, Auth):
[...]
def retranslateUi(self, Auth):
[...]
class Auth(QtWidgets.QDialog, Ui_Auth):
def __init__(self, parent=None):
QtWidgets.QDialog.__init__(self, parent=parent)
self.setupUi(self)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
[...]
def retranslateUi(self, MainWindow):
[...]
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent=parent)
self.setupUi(self)
self.pb_request.clicked.connect(self.launch)
def launch(self):
auth = Auth()
auth.exec_()