PySide MainWindow

vegaseat 2 Tallied Votes 3K Views Share

If you want a window with a menubar, toolbar, statusbar, dock and central widgets, you can use the PySide GUI toolkit's QMainWindow. To use the more traditional box and grid layouts, you simply add a QWidget instance as the central widget. Here is an example.

''' ps_QMainWindow2.py
The PySide QMainWindow has layouts for menubar, toolbar, statusbar,
dock and central widgets.  You can add a QWidget instance as central 
widget for the box and grid layouts.

download PySide (LGPL-licensed version of PyQT) from:
http://qt-project.org/wiki/PySide

tested with Python27 and Python33  by  vegaseat  15jan2013
'''

from PySide.QtCore import *
from PySide.QtGui import *

class MyWidget(QWidget):
    '''
    a QWidget with a typical box layout
    '''
    def __init__(self):
        QWidget.__init__(self)

        self.button1 = QPushButton('button1', self)
        self.button2 = QPushButton('button2', self)
        self.button3 = QPushButton('button3', self)
        self.label = QLabel()
        self.label.setIndent(90)
        
        vb_layout = QVBoxLayout()
        # add the widgets vertically in that order
        vb_layout.addWidget(self.button1)
        vb_layout.addWidget(self.button2)
        vb_layout.addWidget(self.button3)
        vb_layout.addSpacing(5)
        vb_layout.addWidget(self.label)
        self.setLayout(vb_layout)


class MyWindow(QMainWindow):
    '''
    QMainWinodw does not allow box/grid layout, but you can
    make a QWidget instance the central widget to do so
    '''
    def __init__(self):
        # if you want a menubar or statusbar, you have to use
        # QMainWindow since QWidget does not have those
        QMainWindow.__init__(self)
        # setGeometry(x_pos, y_pos, width, height)
        # upper left corner coordinates (x_pos, y_pos)
        self.setGeometry(300, 100, 270, 100)
        self.setWindowTitle('Exploring QMainWindow')

        # exit option for the menu bar File menu
        self.exit = QAction('Exit', self)
        # message for the status bar if mouse is over Exit
        self.exit.setStatusTip('Exit program')
        # newer connect style (PySide/PyQT 4.5 and higher)
        self.exit.triggered.connect(app.quit)

        # create the menu bar
        menubar = self.menuBar()
        file = menubar.addMenu('&File')
        # now add self.exit
        file.addAction(self.exit)

        # create the status bar
        self.statusBar()

        # QWidget or its instance needed for box layout
        widget = MyWidget()
        # make it the central widget of QMainWindow
        self.setCentralWidget(widget)
        self.label = widget.label

        # use lambda to pass a string argument to self.action
        action1 = lambda: self.action('button1')
        widget.button1.clicked.connect(action1)
        action2 = lambda: self.action('button2')
        widget.button2.clicked.connect(action2)
        action3 = lambda: self.action('button3')
        widget.button3.clicked.connect(action3)

    def action(self, s):
        '''
        some action to indicate which button has been clicked
        '''
        sf = '{} clicked'.format(s)
        self.label.setText(sf)


app = QApplication([])
win = MyWindow()
win.show()
app.exec_()