pyqt5设计器的第二个窗口按钮不起作用

时间:2019-10-25 09:17:15

标签: python pyqt pyqt5

我使用qt5设计器创建了两个简单的窗口。在第一个窗口中有一个按钮可以打开第二个窗口。当第二个窗口打开时,其中打印简单文本的按钮不起作用。我读过其他文章,但没有人使用过Qt5 Designer!

    from PyQt5 import QtCore, QtGui, QtWidgets

    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            self.window = MainWindow
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(800, 600)
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
            self.pushButton = QtWidgets.QPushButton(self.centralwidget)
            self.pushButton.setGeometry(QtCore.QRect(290, 230, 181, 29))
            self.pushButton.setObjectName("pushButton")

            self.pushButton.clicked.connect(self.opensecondwindow)

            MainWindow.setCentralWidget(self.centralwidget)
            self.menubar = QtWidgets.QMenuBar(MainWindow)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
            self.menubar.setObjectName("menubar")
            MainWindow.setMenuBar(self.menubar)
            self.statusbar = QtWidgets.QStatusBar(MainWindow)
            self.statusbar.setObjectName("statusbar")
            MainWindow.setStatusBar(self.statusbar)

            self.retranslateUi(MainWindow)
            # QtCore.QMetaObject.connectSlotsByName(MainWindow)

        def retranslateUi(self, MainWindow):
            _translate = QtCore.QCoreApplication.translate
            MainWindow.setWindowTitle(_translate("MainWindow", "First Page"))
            self.pushButton.setText(_translate("MainWindow", "Open Second  Page"))


        def opensecondwindow(self):
            import secondpage as scp
            ui = scp.Ui_MainWindow()
            ui.setupUi(self.window)




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.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(280, 240, 211, 71))
        self.pushButton.setObjectName("pushButton")
        self.pushButton.clicked.connect(self.openfirstwindow)

        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Second Page"))
        self.pushButton.setText(_translate("MainWindow", "Print Data"))

    def openfirstwindow(self):
        print("Hi from second window")



    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_())

任何帮助将不胜感激! 预先感谢

1 个答案:

答案 0 :(得分:0)

首先,两个类具有相同的名称Ui_MainWindow。 一种打印所需内容的选项可能如下所示:

from PyQt5 import QtCore, QtGui, QtWidgets

# class Ui_MainWindow(object):
class Ui_MainWindow_First(object):                                   # + _First
    def setupUi(self, MainWindow):
#        self.window = MainWindow
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(290, 230, 181, 29))
        self.pushButton.setObjectName("pushButton")

        self.pushButton.clicked.connect(self.opensecondwindow)

        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        # QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "First Page"))
        self.pushButton.setText(_translate("MainWindow", "Open Second  Page"))


    def opensecondwindow(self):
#        import secondpage as scp
#        ui = scp.Ui_MainWindow()
#        self.ui = Ui_MainWindow_Second()

### vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
        self.w = QtWidgets.QMainWindow()
        w_ui   = Ui_MainWindow_Second()
        w_ui.setupUi(self.w)
        self.w.show()
        w_ui.pushButton.clicked.connect(self.openfirstwindow)

    def openfirstwindow(self):  
        print("Hi from second window")
### ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


# class Ui_MainWindow(object):
class Ui_MainWindow_Second(object):                                         #  + _Second
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(280, 240, 211, 71))
        self.pushButton.setObjectName("pushButton")
#        self.pushButton.clicked.connect(self.openfirstwindow)

        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Second Page"))
        self.pushButton.setText(_translate("MainWindow", "Print Data"))

#    def openfirstwindow(self):  #(self):
#        print("Hi from second window")



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

enter image description here