通过按钮打开第二个PyQT窗口

时间:2019-05-25 21:39:32

标签: python pyqt5 qt-designer

我使用QTDesigner5制作了2个屏幕,并使用pyuic5将它们转换为.py文件 第一个是我的主屏幕,第二个是学分屏幕。 当我按下“ btnCredits”按钮时,我想显示credits.py屏幕。 我在互联网上发现了类似的问题,但这不是我所需要的。

homescreen.py代码:

# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.uic import loadUi
import qt_about

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
         self.btnCredits = QtWidgets.QPushButton(self.centralwidget)
         self.btnCredits.setGeometry(QtCore.QRect(10, 420, 111, 51))
         self.btnCredits.setObjectName("btnCredits")
         self.btnCredits.clicked.connect(self.showCredits)

         #rest of interface elements like labels and buttons

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        self.btnCreditos.setText(_translate("MainWindow", "Credits"))

    #my method to show credits.py
    def showCredits(self,event):
        #code 

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

还有credits.py

# -*- coding: utf-8 -*-

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        #code

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

    def retranslateUi(self, MainWindow):
        #my code

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

0 个答案:

没有答案