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

15,224

Solution 1

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

Solution 2

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.

Solution 3

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.

Share:
15,224
Shahbaj Sayyad
Author by

Shahbaj Sayyad

Updated on June 19, 2022

Comments

  • Shahbaj Sayyad
    Shahbaj Sayyad almost 2 years

    I'm new to Python GUI programming I'm have trouble making a GUI app. I have a main window with only a button widget on it. What i want to know is how to replace the existing window with a new window when an event occurs (such as a button click).
    An answer to a similar question here Replace CentralWidget in MainWindow, suggests using QStackedWidgets but they did not use Qt Designer to make their GUI apps whereas I have two .py files, one is the main window file and the other of window that i want to show after a button press take place, hence i don't know how to combine these two in my main.py file. For Example my main window looks like this:
    Main Window
    And after clicking on the button it should replace the existing window with this:
    New Window
    I would also like to know if the second window should be of type QStackedWindow, QDialog or QWidget?
    Here is my main.py code

    from PyQt4 import QtGui
    import sys
    import design, design1
    import os
    
    class ExampleApp(QtGui.QMainWindow, design.Ui_MainWindow):
        def __init__(self, parent=None):
            super(ExampleApp, self).__init__(parent)
            self.setupUi(self)
            self.btnBrowse.clicked.connect(self.doSomething)
    
    
        def doSomething(self):
            # Code to replace the main window with a new window
            pass
    
    
    def main():
        app = QtGui.QApplication(sys.argv)
        form = ExampleApp()
        form.show()
        app.exec_()
    
    
    if __name__ == '__main__':
        main()
    
  • Liron Lavi
    Liron Lavi about 6 years
    Why won't you close the other window?