Replacing the existing MainWindow with a new window with Python, PyQt, Qt Designer
<p>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).<br>
An answer to a similar question here <a href="http://stackoverflow.com/questions/13550076/replace-centralwidget-in-mainwindow">Replace CentralWidget in MainWindow</a>, 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:<br>
<a href="http://i.stack.imgur.com/soSsW.png" rel="nofollow"><img src="http://i.stack.imgur.com/soSsW.png" alt="Main Window"></a><br>
And after clicking on the button it should replace the existing window with this:<br>
<a href="http://i.stack.imgur.com/jmgk4.png" rel="nofollow"><img src="http://i.stack.imgur.com/jmgk4.png" alt="New Window"></a><br>
I would also like to know if the second window should be of type QStackedWindow, QDialog or QWidget?<br>
Here is my main.py code  </p>

<pre><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()
</code></pre>

<p>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.</p>

<p>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. :)</p>

<pre><code>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_())
</code></pre>

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

python pyqt qt-designer