## page was renamed from zhArticleTemplate ##language:zh #pragma section-numbers on ''' Menus and Toolbars in PyQt4 ''' ::-- [[zhuyj]] [<>] <> = Menus and Toolbars in PyQt4 PyQt4中的菜单和工具条 = '''PyQt4中的菜单和工具条''' == 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.'' . QMainWindow类提供了一个主的应用程序窗口。它创建一个标准的带有状态栏,工具栏和菜单栏的应用程序 == Statusbar 状态栏 == '' The statusbar is a widget that is used for displaying status information. '' . 状态栏是一个用于显示状态信息的部件 {{{#!python #!/usr/bin/python # statusbar.py 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 get the statusbar, we call the statusBar() method of the QApplication class. The showMessage() displays message on the statusbar.'' . 为了获取状态栏,我们调用QApplication类的statusBar()方法。在状态栏上显示消息用showMessage()方法。 == 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. '' . 菜单栏是GUI应用程序最常见的部分,他是一组位于不同菜单中的命令。在控制台程序中你必须记住所有的神秘的命令,而在这里我们将大部分的命令按照逻辑分组。按照公认的标准可以进一步的减少学习一个新应用程序的时间。 {{{#!python #!/usr/bin/python # menubar.py 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_()) }}} {{{ 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. '' . 首先我们利用QMainWindow类的menuBar() 创建一个菜单栏,然后我们利用AddMenu()方法添加一个菜单。最后我们将action对象插入到文件菜单中。 == Toolbar 工具栏 == '' Menus group all commands that we can use in an application. Toolbars provide a quick access to the most frequently used commands.'' . 菜单聚合了我们在一个应用程序中要用的的所有的命令。工具栏提供了一个最经常使用命令的快速链接。 {{{#!python #!/usr/bin/python # toolbar.py 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 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.'' . GUI应用程序通过命令来控制,这些命令可以通过菜单,上下文菜单,工具栏或者一个快捷方式来执行。PyQt简化了actions传入的开发。一个action对象可以具有菜单文本,图标,快捷方式,状态栏提示,"这是什么?"提示和工具提示。在我们的例子里,我们定义了一个具有图标,工具提示和快捷方式的action对象。 {{{ self.connect(self.exit, QtCore.SIGNAL('triggered()'), QtCore.SLOT('close()')) }}} '' Here we connect the action's triggered() signal to the predefined close() signal.'' . 这里我们将triggered()信号的动作与预先定义的close()信号连接。 {{{ self.toolbar = self.addToolBar('Exit') self.toolbar.addAction(self.exit) }}} '' Here we create a toolbar and plug and action object into it. '' . 这里我们创建一个工具栏并将action对象插入进去。 toolbar {{attachment:toolbar.jpg}} 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.'' . 在这节的最后一个例子里,我们将创建一个菜单栏,工具栏和状态栏。我们也会创建一个中央的部件。 {{{#!python #!/usr/bin/python # mainwindow.py 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_()) }}} {{{ 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.'' . 这里我们创建一个文本编辑插件,我们将他设置为QMainWindow的中央插件。中央插件将会占据剩下的所有的空间。 mainwindow {{attachment:mainwindow.jpg}} Figure: mainwindow