PyQt-多次打开同一页面

时间:2019-10-02 18:07:38

标签: python pyqt pyqt5

我想从我的Index.py页面打开几次Form.py页面。 目前,当我尝试第二次打开Form时,第一个Form页面关闭。

有我的Index.py代码:

from PyQt5 import QtCore, QtGui, QtWidgets
from Form import Ui_FormWindow

class Ui_MainWindow(object):

    #The function to open the Form.py
    def open(self):
        self.window = QtWidgets.QMainWindow()
        self.ui = Ui_FormWindow()
        self.ui.setupUi(self.window)
        self.window.show()


    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(230, 180, 93, 28))
        self.pushButton.setObjectName("pushButton")

        self.pushButton.clicked.connect(self.open)

        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
        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", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))


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

这是我要打开几次的页面,Form.py:

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_FormWindow(object):
    def setupUi(self, FormWindow):
        FormWindow.setObjectName("FormWindow")
        FormWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(FormWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(250, 190, 113, 22))
        self.lineEdit.setObjectName("lineEdit")
        FormWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(FormWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
        self.menubar.setObjectName("menubar")
        FormWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(FormWindow)
        self.statusbar.setObjectName("statusbar")
        FormWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, FormWindow):
        _translate = QtCore.QCoreApplication.translate
        FormWindow.setWindowTitle(_translate("FormWindow", "FormWindow"))
        self.lineEdit.setText(_translate("FormWindow", "Page to open"))


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


我检查了此answer,但不了解如何在我的代码中使用它。 我还编辑了我的问题,以使它成为一个可复制的示例,但我的英语并不完美,我担心无法完全理解我在mre tutotial上读到的内容,对不起

1 个答案:

答案 0 :(得分:0)

我已经有一段时间没做过QT了,但这是否可能是您再次实例化QtWidgets.QMainWindow的事实?那是我的第一个猜测。