Home > PyQt5, python > PyQt5: QMainWindow

PyQt5: QMainWindow

8 Marzo 2019

Torna all’indice degli appunti

QMainWindow

La classe QmainWindow, fornisce la finestra principale dell’applicazione.
La QMainWindow ha il proprio Layout sul quale è possibile aggiungere:
Toolbars (QToolBars), DockWidgets (QDockWidgets), MenuBar (QMenuBar) e una StatusBar (QStatusBar).

Il layout della QMainWindow ha un’area centrale che può ospitare ogni tipo di widget.

Attenzione:

Non è consentito creare una QMainWindow senza un central widget.
Anche se vuoto, come semplice segnaposto, è nesessario prevedere tale central widget.

Il central widget si aggiunge con il metodo setCentralWidget(widget):

from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QWidget, QApplication
import sys


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.central_widget = FormWidget(self) 
        self.setCentralWidget(self.central_widget) 


class FormWidget(QWidget):
    def __init__(self, parent):
        super(FormWidget, self).__init__(parent)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec_())

TITOLO

Per aggiungere un titolo alla QMainWindow, si utilizza il metodo setWindowTitle(title).

DIMENSIONI

Per modificare le dimensioni della QMainWindow, si utilizza invece il metodo setGeometry(left, top, width, height), dove gli argomenti sono appunto:

left: posizione rispetto al bordo sinistro dello schermo;
top: posizione rispetto al limite superiore;
width: larghezza QMainWindow;
height: altezza QMainWindow;

esempio:

from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QWidget, QApplication
import sys


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setWindowTitle("QMainWindow Example")
        self.central_widget = FormWidget(self) 
        self.setCentralWidget(self.central_widget)
        left, top, width, height = 300, 300, 400, 400
        self.setGeometry(left, top, width, height)


class FormWidget(QWidget):
    def __init__(self, parent):
        super(FormWidget, self).__init__(parent)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec_())

POSIZIONE

Se vogliamo che la QMainWindow sia centrata nello schermo, possiamo utilizzare il metodo resize per il dimensionamento della stessa ed ometterne il posizionamento e, di default, verrà posizionata al centro dello schermo.

from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QWidget, QApplication
import sys


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setWindowTitle("QMainWindow Example")
        self.central_widget = FormWidget(self) 
        self.setCentralWidget(self.central_widget)
        self.resize(400, 400)


class FormWidget(QWidget):
    def __init__(self, parent):
        super(FormWidget, self).__init__(parent)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec_())

STATUS BAR

Aggiungere una statusbar è molto semplice, si invoca il metodo statusBar() di QMainWindow, che crea un oggetto QStatusBar.
Per mostrare messaggi su di essa, si utilizza il metodo showMessage(message):

...


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setWindowTitle("QMainWindow Example")
        self.central_widget = FormWidget(self) 
        self.setCentralWidget(self.central_widget)
        self.resize(400, 400)
        self.status_bar = self.statusBar()
        self.status_bar.showMessage("...Ready")

...

MENU BAR

Per aggiungere una menu bar si invoca il metodo menuBar() di QMainWindow, che crea un oggetto QMenuBar. Poi, per aggiungere Menu alla MenuBar si utilizza il metodo addMenu(label), che restituisce un oggetto QMenu.
Per aggiungere voci ad un oggetto QMenu, si utilizza il metodo addAction(label), che ritorna invece un oggetto QAction:

...


        super(MainWindow, self).__init__(parent)
        self.setWindowTitle("QMainWindow Example")
        self.central_widget = FormWidget(self) 
        self.setCentralWidget(self.central_widget)
        self.resize(400, 400)
        # Status Bar
        self.status_bar = self.statusBar()
        self.status_bar.showMessage("...Ready")
        # Menu Bar
        self.menu_bar = self.menuBar()
        self.menu_file = self.menu_bar.addMenu("File")
        self.menu_file.addAction("New")
        self.menu_file.addAction("Exit")
        self.menu_info = self.menu_bar.addMenu("Info")
        self.menu_info.addAction("About")

...

Ovviamente è possibile aggiungere icone alle voci di menu:

...

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        ...
        # EXIT Action
        exit_icon = QIcon('exit24.png')
        exit_action = QAction(exit_icon, 'Exit', parent=self)
        exit_action.setShortcut('Ctrl+Q')
        exit_action.setStatusTip('Exit application')
        exit_action.triggered.connect(self.on_exit)
        self.menu_file.addAction(exit_action)

        self.menu_info = self.menu_bar.addMenu("Info")
        self.menu_info.addAction("About")

...

Torna all’indice degli appunti

Categorie:PyQt5, python Tag: ,
I commenti sono chiusi.