Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Pyside2: How can load new window inside Main Window?



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



  • @Marcone
    Depends what main.py & new.py` 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?:

    main.py & new.py`

    main.py and new.py 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 new.py without being in a new window

    mean?



  • @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 Program.py and import two files: untitled.py (Main) and newform.py (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__()
            self.setupUi(self)
            self.bt2.clicked.connect(self.fun)
    
            def fun(self):
                print('Go to form 1')
    
    class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
        def __init__(self):
            super(MainWindow, self).__init__()
            self.setupUi(self)
            self.bt1.clicked.connect(self.fun)
            
        def fun(self):
            print('Go to form 2')
            
    if __name__ == "__main__":
        app = QtWidgets.QApplication(sys.argv)
        window = MainWindow()
        window.show()
        sys.exit(app.exec_())
    

Log in to reply