Search code examples
python-2.7pysidemenubar

How to change a content in a window in PySide?


I want to click on a menubar "Tools" menu to change my window content completely. How do I do this with PySide? Should I call QAction and set new widget as a central one with a parent of an old window? I'm beginner in python and english too. So far I've created only one-window application.


Solution

  • Firstly, I would define each tool within its own subclass of a QWidget. Secondly, I would add an instance of each tool widget thus created to the layout of the central widget of the main window. Finally, I would add actions to the menuBar and connect them to methods to show and hide the tools as desired.

    Below is an example to show how this can be done with 2 different tools:

    from PySide import QtGui 
    import sys
    
    class myApplication(QtGui.QMainWindow):
        def __init__(self, parent=None):
            super(myApplication, self).__init__(parent)
    
            self.setWindowTitle('No Tool is Selected')
    
            #---- create instance of each tool widget ----
    
            self.tool1 = Tool1(self)
            self.tool2 = Tool2(self)
    
            #---- layout for central widget ----
    
            centralWidget = QtGui.QWidget()
            centralLayout = QtGui.QGridLayout()
            centralLayout.addWidget(self.tool1, 0, 0)
            centralLayout.addWidget(self.tool2, 1, 0)
            centralWidget.setLayout(centralLayout)
    
            self.setCentralWidget(centralWidget)  
    
            #---- set the menu bar ----
    
            contentMenu = self.menuBar().addMenu(("Tools"))
            contentMenu.addAction('show Tool 1', self.show_Tool1)
            contentMenu.addAction('show Tool 2', self.show_Tool2)
            contentMenu.addAction('show All', self.show_All)
    
        def show_Tool1(self):
            self.tool1.show()
            self.tool2.hide()
            self.setWindowTitle('Tool #1 is Selected')
    
        def show_Tool2(self):
            self.tool1.hide()
            self.tool2.show()
            self.setWindowTitle('Tool #2 is Selected')
    
        def show_All(self):
            self.tool1.show()
            self.tool2.show()
            self.setWindowTitle('All Tools are Selected')
    
    class Tool1(QtGui.QWidget):
        def __init__(self, parent=None):
            super(Tool1, self).__init__(parent)
    
            layout = QtGui.QGridLayout()
            layout.addWidget(QtGui.QPushButton('Tool #1'))
            self.setLayout(layout)
            self.hide()
    
    class Tool2(QtGui.QWidget):
        def __init__(self, parent=None):
            super(Tool2, self).__init__(parent)
    
            layout = QtGui.QGridLayout()
            layout.addWidget(QtGui.QTextEdit('Tool #2'))
            self.setLayout(layout)
            self.hide()
    
    if __name__ == '__main__':
    
        app = QtGui.QApplication(sys.argv)
        instance = myApplication()  
        instance.show()    
        sys.exit(app.exec_())
    

    Which results in:

    enter image description here