Important: Please read the Qt Code of Conduct -

Pyside2: How can load new window inside Main Window?

  • Example: I have two files: and I want that when I click a button on it loads without being in a new window. And when I click on a button inside it goes back to Could someone give me a simple code example of how to do this?

  • @Marcone
    Depends what &` have in them, what widgets they define and how, etc.

    It is often the case with this kind of question that you should consider using a QStackedWidget. But as I say, depends what you have.

  • @JonB said in Pyside2: How can load new window inside Main Window?: &` and are independent MainWindows form. Not QStackedWidget case.

  • @Marcone
    Most likely the wrong way to do things. And still likely that you should be using QStackedWidget.

    Then if they are both of class MainWindow, what does your

    it loads without being in a new window


  • @JonB I want create every window in separated .ui forms and call everyone when needed. How can I make it? can you send simple example?

  • @Marcone
    You can paste your code if you wish, not send me something. I am not going to send you anything.

    I have no idea what you mean by having separated forms, and each of type QMainWindow, but displaying each one without it being a new window. Showing code won't explain that to me.

    Since you won't consider QStackedWidget, at the end of the day you will presumably want pushbutton code which just show()s the other window and hide()s the current window, and that's all there is to say.

  • My code: This file is called and import two files: (Main) and (New window to load)

    # -*- coding: utf-8 -*- 
    import sys
    from PySide2 import QtCore, QtGui, QtWidgets
    from untitled import Ui_MainWindow
    from newform import Ui_Form
    class Form2(QtWidgets.QMainWindow, Ui_Form):
        def __init__(self):
            super(Form2, self).__init__()
            def fun(self):
                print('Go to form 1')
    class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
        def __init__(self):
            super(MainWindow, self).__init__()
        def fun(self):
            print('Go to form 2')
    if __name__ == "__main__":
        app = QtWidgets.QApplication(sys.argv)
        window = MainWindow()

Log in to reply