Menus and Toolbars in PyQt4

::-- zhuyj [2008-06-06 02:12:56]

1. Menus and Toolbars in PyQt4 PyQt4中的菜单和工具条

PyQt4中的菜单和工具条

1.1. 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.

1.2. Statusbar 状态栏

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

   1 #!/usr/bin/python
   2 
   3 # statusbar.py 
   4 
   5 import sys
   6 from PyQt4 import QtGui
   7 
   8 class MainWindow(QtGui.QMainWindow):
   9     def __init__(self):
  10         QtGui.QMainWindow.__init__(self)
  11 
  12         self.resize(250, 150)
  13         self.setWindowTitle('statusbar')
  14 
  15         self.statusBar().showMessage('Ready')
  16 
  17 
  18 app = QtGui.QApplication(sys.argv)
  19 main = MainWindow()
  20 main.show()
  21 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.

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.

   1 #!/usr/bin/python
   2 
   3 # menubar.py 
   4 
   5 import sys
   6 from PyQt4 import QtGui, QtCore
   7 
   8 class MainWindow(QtGui.QMainWindow):
   9     def __init__(self):
  10         QtGui.QMainWindow.__init__(self)
  11 
  12         self.resize(250, 150)
  13         self.setWindowTitle('menubar')
  14 
  15         exit = QtGui.QAction(QtGui.QIcon('icons/exit.png'), 'Exit', self)
  16         exit.setShortcut('Ctrl+Q')
  17         exit.setStatusTip('Exit application')
  18         self.connect(exit, QtCore.SIGNAL('triggered()'), QtCore.SLOT('close()'))
  19 
  20         self.statusBar()
  21 
  22         menubar = self.menuBar()
  23         file = menubar.addMenu('&File')
  24         file.addAction(exit)
  25 
  26 app = QtGui.QApplication(sys.argv)
  27 main = MainWindow()
  28 main.show()
  29 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.

1.4. Toolbar 工具栏

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

   1 #!/usr/bin/python
   2 
   3 # toolbar.py 
   4 
   5 import sys
   6 from PyQt4 import QtGui, QtCore
   7 
   8 class MainWindow(QtGui.QMainWindow):
   9     def __init__(self):
  10         QtGui.QMainWindow.__init__(self)
  11 
  12         self.resize(250, 150)
  13         self.setWindowTitle('toolbar')
  14 
  15         self.exit = QtGui.QAction(QtGui.QIcon('icons/exit.png'), 'Exit', self)
  16         self.exit.setShortcut('Ctrl+Q')
  17         self.connect(self.exit, QtCore.SIGNAL('triggered()'), QtCore.SLOT('close()'))
  18 
  19         self.toolbar = self.addToolBar('Exit')
  20         self.toolbar.addAction(self.exit)
  21 
  22 
  23 app = QtGui.QApplication(sys.argv)
  24 main = MainWindow()
  25 main.show()
  26 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.

toolbar.jpg Figure: toolbar

1.5. 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.

   1 #!/usr/bin/python
   2 
   3 # mainwindow.py 
   4 
   5 import sys
   6 from PyQt4 import QtGui, QtCore
   7 
   8 class MainWindow(QtGui.QMainWindow):
   9     def __init__(self):
  10         QtGui.QMainWindow.__init__(self)
  11 
  12         self.resize(350, 250)
  13         self.setWindowTitle('mainwindow')
  14 
  15         textEdit = QtGui.QTextEdit()
  16         self.setCentralWidget(textEdit)
  17 
  18         exit = QtGui.QAction(QtGui.QIcon('icons/exit.png'), 'Exit', self)
  19         exit.setShortcut('Ctrl+Q')
  20         exit.setStatusTip('Exit application')
  21         self.connect(exit, QtCore.SIGNAL('triggered()'), QtCore.SLOT('close()'))
  22 
  23         self.statusBar()
  24 
  25         menubar = self.menuBar()
  26         file = menubar.addMenu('&File')
  27         file.addAction(exit)
  28 
  29         toolbar = self.addToolBar('Exit')
  30         toolbar.addAction(exit)
  31 
  32 
  33 app = QtGui.QApplication(sys.argv)
  34 main = MainWindow()
  35 main.show()
  36 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.

mainwindow.jpg Figure: mainwindow

Menus_and_Toolbars_菜单与工具条 (last edited 2010-11-16 01:15:30 by zhuyj)