📜 ⬆️ ⬇️

PyQt4 - Menus and Toolbars

Main window
The QMainWindow class is the main application window. With it, you can create a classic view with a status bar, toolbars and menus.

Status bar
The status bar is a widget that is used to display status information.
#!/usr/bin/python

import sys
from PyQt4 import QtGui

class MainWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)

self.resize(250, 150)
self.setWindowTitle('statusbar')

self.statusBar().showMessage('Ready')

app = QtGui.QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())


self.statusBar().showMessage('Ready')

To create a status bar, we call the statusBar () method of the QApplication class. ShowMessage () displays a message on the status bar.

Menu
The menu is one of the most prominent parts of a GUI application. This is a group of commands located in different menus. While in console applications you need to remember all the secret commands, here you can access most of the commands grouped logically. This is an accepted standard that reduces the time to learn a new application.
#!/usr/bin/python

import sys
from PyQt4 import QtGui, QtCore

class MainWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)

self.resize(250, 150)
self.setWindowTitle('menubar')

exit = QtGui.QAction(QtGui.QIcon('icons/exit.png'), 'Exit', self)
exit.setShortcut('Ctrl+Q')
exit.setStatusTip('Exit application')
self.connect(exit, QtCore.SIGNAL('triggered()'), QtCore.SLOT('close()'))

self.statusBar()

menubar = self.menuBar()
file = menubar.addMenu('&File')
file.addAction(exit)

app = QtGui.QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())

self.statusBar().showMessage('Ready')

First, we create a menu using the QMainWindow class menuBar () method. Then, using the addMenu () method, add a File menu item, then connect the exit object to the created item.
Toolbar
The menu combines all the commands that we can use in the application. Toolbars, in turn, provide quick access to the most frequently used commands.
#!/usr/bin/python

import sys
from PyQt4 import QtGui, QtCore

class MainWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)

self.resize(250, 150)
self.setWindowTitle('toolbar')

self.exit = QtGui.QAction(QtGui.QIcon('icons/exit.png'), 'Exit', self)
self.exit.setShortcut('Ctrl+Q')
self.connect(self.exit, QtCore.SIGNAL('triggered()'), QtCore.SLOT('close()'))

self.toolbar = self.addToolBar('Exit')
self.toolbar.addAction(self.exit)

app = QtGui.QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())

self.exit = QtGui.QAction(QtGui.QIcon('icons/exit.png'), 'Exit', self)
self.exit.setShortcut('Ctrl+Q')

GUI applications are controlled by commands, and these commands can be launched from the menu, context menu, toolbar, or by using hotkeys. PyQt simplifies development with actions. The action object can have menu text, an icon, a shortcut (keyboard shortcut), status text, the text “What's This?” And a tooltip. In our example, we define an action object with an icon, a shortcut, and a tooltip.
self.connect(self.exit, QtCore.SIGNAL('triggered()'),
QtCore.SLOT('close()'))

Here we connect the triggered () signal of the action object with the predefined close () signal.
self.toolbar = self.addToolBar('Exit')
self.toolbar.addAction(self.exit)

Create a toolbar and set an action object on it.
Toolbar
')
Combine together
In the last example, we will create a window and place a menu, a toolbar and a status bar on it, and also add a central widget.
#!/usr/bin/python

import sys
from PyQt4 import QtGui, QtCore

class MainWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)

self.resize(350, 250)
self.setWindowTitle('mainwindow')

textEdit = QtGui.QTextEdit()
self.setCentralWidget(textEdit)

exit = QtGui.QAction(QtGui.QIcon('icons/exit.png'), 'Exit', self)
exit.setShortcut('Ctrl+Q')
exit.setStatusTip('Exit application')
self.connect(exit, QtCore.SIGNAL('triggered()'), QtCore.SLOT('close()'))

self.statusBar()

menubar = self.menuBar()
file = menubar.addMenu('&File')
file.addAction(exit)

toolbar = self.addToolBar('Exit')
toolbar.addAction(exit)

app = QtGui.QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())

self.toolbar = self.addToolBar('Exit')
self.toolbar.addAction(self.exit)

Here we create a QtextEdit widget that we set central on the QMainWindow. The central widget takes up all the free space.
mainwindow

Source: https://habr.com/ru/post/31685/


All Articles