需要在Pyqt5中的2个主窗口之间切换[重复]

问题描述 投票:0回答:1

这个问题与以下内容完全相同:

我创建了2个单独的MainWindow(在2个不同的程序中)。现在我需要在这两个窗口之间切换。示例:单击MainWindow1中的按钮时,应打开MainWindow2并关闭MainWindow1,反之亦然。请帮忙!

python layout pyqt pyqt5
1个回答
0
投票

试试吧:

import sys
from PyQt5 import Qt

class MainWindow1(Qt.QMainWindow):
    def __init__(self):
        Qt.QMainWindow.__init__(self)

        self.mainWindow1()


    def mainWindow1(self):
        self.setFixedSize(300, 300)
        self.setStyleSheet('background-color : rgb(255,255,255);')
        self.setWindowTitle('MainWindow1')

        self.pushButton = Qt.QPushButton(self)
        self.pushButton.setStyleSheet('background-color: rgb(255,0,0); color: #fff')
        self.pushButton.setText('Click me!')
        self.pushButton.clicked.connect(self.A)

    def A(self):
        self.cams = MainWindow2()
        self.cams.show()
        self.close()

class MainWindow2(Qt.QMainWindow):
    def __init__(self):
        Qt.QMainWindow.__init__(self)

        self.setFixedSize(500, 500)
        self.setStyleSheet('background-color : rgb(255,0,0);')
        self.setWindowTitle('MainWindow2')

        self.pushButton = Qt.QPushButton(self)
        self.pushButton.setStyleSheet('background-color: rgb(0,0,255); color: #fff')
        self.pushButton.setText('Click me!')
        self.pushButton.clicked.connect(self.B)

    def B(self):
        self.cams = MainWindow1()
        self.cams.show()
        self.close()          


if __name__ == '__main__':
    app = Qt.QApplication(sys.argv)
    w   = MainWindow1()
    w.show()
    sys.exit(app.exec_())
© www.soinside.com 2019 - 2024. All rights reserved.