Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Putting it together

Menus and Toolbars in PyQt4

Google_protectAndRun("render_ads.js::google_render_ad", google_handleError, google_render_ad); google_protectAndRun("ads_core.google_render_ad", google_handleError, google_render_ad); Main Window

The QMainWindow class provides a main application window. This enables to create the classic application skeleton with a statusbar, toolbars and a menubar.

Statusbar

The statusbar is a widget that is used for displaying status information.

#!/usr/bin/python # statusbar.py import sysfrom 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 get the statusbar, we call the statusBar() method of the QApplication class. The showMessage() displays message on the statusbar.

Menubar

A menubar is one of the most visible parts of the GUI application. It is a group of commands located in various menus. While in console applications you had to remember all those arcane commands, here we have most of the commands grouped into logical parts. There are accepted standards that further reduce the amount of time spending to learn a new application.

#!/usr/bin/python # menubar.py import sysfrom 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_())

 

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

First we create a menubar with the menuBar() method of the QMainWindow class. Then we add a menu with the AddMenu() method. In the end we plug the action object into the file menu.

Toolbar

Menus group all commands that we can use in an application. Toolbars provide a quick access to the most frequently used commands.

#!/usr/bin/python # toolbar.py import sysfrom 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 with commands. These commands can be launched from a menu, a context menu, a toolbar or with a shortcut. PyQt simplifies development with the introduction of actions. An action object can have menu text, an icon, a shortcut, status text, "What's This?" text and a tooltip. In our example, we define an action object with an icon, a tooltip and a shortcut.



self.connect(self.exit, QtCore.SIGNAL('triggered()'), QtCore.SLOT('close()'))

Here we connect the action's triggered() signal to the predefined close() signal.

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

Here we create a toolbar and plug and action object into it.


Figure: toolbar

Putting it together

In the last example of this section, we will create a menubar, toolbar and a statusbar. We will also create a central widget.

#!/usr/bin/python # mainwindow.py import sysfrom 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_())

 

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

Here we create a text edit widget. We set it to be the central widget of the QMainWindow. The central widget will occupy all space that is left.


Figure: mainwindow

 


Date: 2016-01-14; view: 732


<== previous page | next page ==>
Paper on future work on flexibilities in the IP system Evaluation reports on six completed projects | Example of Subjectivity of OC
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.007 sec.)