Replacing the existing MainWindow with a new window with Python, PyQt, Qt Designer

允我心安 提交于 2019-12-04 05:41:58

You probably don't want to actually create and delete a bunch of windows, but if you really want to, you could do it like this

def doSomething(self):
    # Code to replace the main window with a new window
    window = OtherWindow()
    window.show()
    self.close()

The in the OtherWindow class

class OtherWindow(...):
    ...
    def doSomething(self):
        window = ExampleApp()
        window.show()
        self.close()

You actually probably don't want to do this. It would be much better if you simply created 1 main window, with a QStackedWidget and put the different controls and widgets on different tabs of the stacked widget and just switch between them in the same window.

You can use the QStackedWindow to create a entire new window and hten connect it to the main window through onclick() event.

button.clicked.connect(self.OtherWindow)

Or else you can simply use the

class OtherWindow(self):
    Owindow = OtherWindow()  
    Owindow.show()

def main():
    app = QtGui.QApplication(sys.argv)
    form = ExampleApp()
    form.show()
    app.exec_()


if __name__ == '__main__':
    main()

Here is a simple example, you just have to use your own logistic but it's a simple way to represent what you are looking for.

You can use QWindow instead of QWidgets if your prefer, or different layout to dispose your "objects" or whatever. Maybe change a bit how to add items inside layout if not widget... things like that. :)

from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QStackedLayout
from PyQt5.QtWidgets import QWidget, QApplication

class MyWindow(QMainWindow):

    front_wid = None

    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)

        # MAIN WINDOW size
        self.setFixedSize(200,200)

        # CENTRAL WIDGET
        self.central_wid = QWidget()
        self.layout_for_wids = QStackedLayout()

        # BUTTON TO SWITCH BETWEEN WIDGETS
        self.btn_switch = QPushButton("Switch")
        self.btn_switch.clicked.connect(self.switch_wids)
        self.btn_switch.setFixedSize(50,50)
        self.btn_switch

        # 2 WIDGETS
        self.wid1 = QWidget()
        self.wid1.setStyleSheet("""background: blue;""")
        self.wid1.setFixedSize(200,200)
        self.wid1.move(100, 100)
        self.wid2 = QWidget()
        self.wid2.setStyleSheet("""background: green;""")
        self.wid2.setFixedSize(200, 200)
        self.wid2.move(100, 100)

        # LAYOUT CONTAINER FOR WIDGETS AND BUTTON
        self.layout_for_wids.addWidget(self.btn_switch)
        self.layout_for_wids.addWidget(self.wid1)
        self.layout_for_wids.addWidget(self.wid2)

        # ENTERING LAYOUT
        self.central_wid.setLayout(self.layout_for_wids)

        # CHOOSE YOUR CENTRAL WIDGET
        self.setCentralWidget(self.central_wid)

        # WHICH WIDGET IS ON THE FRONT
        self.front_wid = 1

    def switch_wids(self):

        # LOGIC TO SWITCH
        if self.front_wid == 1:
            self.wid1.hide()
            self.wid2.show()
            self.front_wid = 2
        else:
            self.wid1.show()
            self.wid2.hide()
            self.front_wid = 1



if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    app.setApplicationName('MyWindow')

    main = MyWindow()
    main.resize(222, 222)
    main.show()

    sys.exit(app.exec_())

By the way it's PyQt5, if you want to use PyQt4 you have to change the imports that's all.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!