无法引发内部对话框事件pyqt5

时间:2018-11-06 04:41:06

标签: python pyqt pyqt5 python-3.6

我使用pyqt设计器生成UI。我从主窗口中打开了一个对话框,无法引发对话框事件。我已经做了足够的工作来在这里重现我的问题。

完整代码在这里。

from PyQt5 import QtCore, QtGui, QtWidgets
import Ui_subwindow


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(410, 130, 160, 80))
        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.main_test = QtWidgets.QPushButton(self.verticalLayoutWidget)
        self.main_test.setObjectName("main_test")
        self.verticalLayout.addWidget(self.main_test)
        self.dialog = QtWidgets.QDialog()
        MainWindow.setCentralWidget(self.centralwidget)
        self.main_test.clicked.connect(self.show_dialog)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.main_test.setText(_translate("MainWindow", "open dialog"))

    def show_dialog(self):
        dui = Ui_subwindow.Ui_Dialog()
        dui.setupUi(self.dialog)
        self.dialog.show()


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

在对话框中,我使用了一个引发事件,如下所示:

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(400, 300)
        self.gridLayoutWidget = QtWidgets.QWidget(Dialog)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(160, 110, 160, 80))
        self.gridLayoutWidget.setObjectName("gridLayoutWidget")
        self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
        self.gridLayout.setContentsMargins(0, 0, 0, 0)
        self.gridLayout.setObjectName("gridLayout")
        self.sub_test = QtWidgets.QPushButton(self.gridLayoutWidget)
        self.sub_test.setObjectName("sub_test")
        self.gridLayout.addWidget(self.sub_test, 0, 0, 1, 1)
        self.sub_test.clicked.connect(self.raised)
        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.sub_test.setText(_translate("Dialog", "raise console"))

    def raised(self):
        print("raised")

在以下几篇文章中,我添加了方法事件侦听器,这些方法在分别调用时会触发。但当一个窗口连接到另一个窗口时则不会。

文件名:Ui_main.pyUi_subwindow.py

1 个答案:

答案 0 :(得分:1)

问题在于dui是一个局部变量,由于无法调用提升函数,因此将其消除,解决方案是使dui成为类的属性。

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(410, 130, 160, 80))
        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.main_test = QtWidgets.QPushButton(self.verticalLayoutWidget)
        self.main_test.setObjectName("main_test")
        self.verticalLayout.addWidget(self.main_test)

        self.dialog = QtWidgets.QDialog()
        self.dui = Ui_subwindow.Ui_Dialog() # <--- add self.
        self.dui.setupUi(self.dialog) # <---

        MainWindow.setCentralWidget(self.centralwidget)
        self.main_test.clicked.connect(self.show_dialog)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.main_test.setText(_translate("MainWindow", "open dialog"))

    def show_dialog(self):
        self.dialog.show()