原文:http://zetcode.com/gui/pyqt5/menustoolbars/python
咱们将在这部分教程中建立菜单与工具栏。一个菜单就是位于菜单栏中的一组命令。应用的工具栏放置了带有按钮的经常使用命令。web
QMainWindow
类提供了一个主程序窗体。经过它能够建立带有状态栏、工具栏与菜单栏的传统应用程序。app
状态栏是用于显示状态信息的控件。工具
#!/usr/bin/python3 # -*- coding: utf-8 -*- """ ZetCode PyQt5 tutorial This program creates a statusbar. author: Jan Bodnar website: zetcode.com last edited: January 2015 """ import sys from PyQt5.QtWidgets import QMainWindow, QApplication class Example(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.statusBar().showMessage('Ready') self.setGeometry(300, 300, 250, 150) self.setWindowTitle('Statusbar') self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())
能够经过QMainWindow
建立状态栏控件。ui
self.statusBar().showMessage('Ready')
咱们须要调用QtGui.QMainWindow
的statusBar()
方法来建立状态栏。第一次调用该方法会建立一个状态栏对象,以后的调用都会返回这个状态栏对象。showMessage()
会将消息展现在状态栏。3d
菜单栏是GUI程序的标配。它是一组位于不一样菜单内的命令集。(Mac系统会以不一样的方式处理菜单栏,但添加menubar.setNativeMenuBar(False)
这行代码后能够获得一致的结果。)code
#!/usr/bin/python3 # -*- coding: utf-8 -*- """ ZetCode PyQt5 tutorial This program creates a menubar. The menubar has one menu with an exit action. author: Jan Bodnar website: zetcode.com last edited: January 2015 """ import sys from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication from PyQt5.QtGui import QIcon class Example(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): exitAction = QAction(QIcon('exit.png'), '&Exit', self) exitAction.setShortcut('Ctrl+Q') exitAction.setStatusTip('Exit application') exitAction.triggered.connect(qApp.quit) self.statusBar() menubar = self.menuBar() fileMenu = menubar.addMenu('&File') fileMenu.addAction(exitAction) self.setGeometry(300, 300, 300, 200) self.setWindowTitle('Menubar') self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())
在这个例子中咱们建立了一个带有一个菜单的菜单栏。这个菜单中只有一个动做,当触发后会使程序中止。这里也建立了状态栏。能够使用Ctrl+Q快捷键触发这个动做。对象
exitAction = QAction(QIcon('exit.png'), '&Exit', self) exitAction.setShortcut('Ctrl+Q') exitAction.setStatusTip('Exit application')
QAction
是菜单栏、工具栏或自定义快捷键中能够执行的动做的抽象表示。上面这三行代码建立了一个带有特定图标与‘Exit’标签的动做,并且还为这个动做定义了一个快捷键。第三个代码为这个动做设置了状态提示,当鼠标悬停在这个菜单项上时状态提示会显示在状态栏。blog
exitAction.triggered.connect(qApp.quit)
当点击这个动做时会发出triggered信号。这个信号链接到了QApplication
的quit()
方法。从而使程序中止。教程
menubar = self.menuBar() fileMenu = menubar.addMenu('&File') fileMenu.addAction(exitAction)
menuBar()
方法会建立一个菜单栏。咱们在菜单栏中建立了一个file菜单并为其添加了exitAction。
菜单为应用程序中的全部命令进行分组。工具栏为经常使用命令提供了快速的访问方式。
#!/usr/bin/python3 # -*- coding: utf-8 -*- """ ZetCode PyQt5 tutorial This program creates a toolbar. The toolbar has one action, which terminates the application, if triggered. author: Jan Bodnar website: zetcode.com last edited: January 2015 """ import sys from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication from PyQt5.QtGui import QIcon class Example(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): exitAction = QAction(QIcon('exit24.png'), 'Exit', self) exitAction.setShortcut('Ctrl+Q') exitAction.triggered.connect(qApp.quit) self.toolbar = self.addToolBar('Exit') self.toolbar.addAction(exitAction) self.setGeometry(300, 300, 300, 200) self.setWindowTitle('Toolbar') self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())
在这个例子中咱们建立了一个简单的工具栏。这个工具栏中有一个退出动做,当触发后会使程序退出。
exitAction = QAction(QIcon('exit24.png'), 'Exit', self) exitAction.setShortcut('Ctrl+Q') exitAction.triggered.connect(qApp.quit)
与上面菜单栏示例相似,咱们建立了一个QAction对象。这个对象也有标签、图标和快捷键。QtGui.QMainWindow
的quit()
方法与triggered信号相连。
self.toolbar = self.addToolBar('Exit') self.toolbar.addAction(exitAction)
这里咱们建立了一个工具栏并在其中添加了一个QAction对象。
在这节教程的最后,咱们将建立一个菜单栏、工具栏与状态栏。咱们也会建立一个中心部件。
#!/usr/bin/python3 # -*- coding: utf-8 -*- """ ZetCode PyQt5 tutorial This program creates a skeleton of a classic GUI application with a menubar, toolbar, statusbar, and a central widget. author: Jan Bodnar website: zetcode.com last edited: January 2015 """ import sys from PyQt5.QtWidgets import QMainWindow, QTextEdit, QAction, QApplication from PyQt5.QtGui import QIcon class Example(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): textEdit = QTextEdit() self.setCentralWidget(textEdit) exitAction = QAction(QIcon('exit24.png'), 'Exit', self) exitAction.setShortcut('Ctrl+Q') exitAction.setStatusTip('Exit application') exitAction.triggered.connect(self.close) self.statusBar() menubar = self.menuBar() fileMenu = menubar.addMenu('&File') fileMenu.addAction(exitAction) toolbar = self.addToolBar('Exit') toolbar.addAction(exitAction) self.setGeometry(300, 300, 350, 250) self.setWindowTitle('Main window') self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())
这段代码建立了一个包含菜单栏、工具栏与状态栏的传统GUI程序。
textEdit = QTextEdit() self.setCentralWidget(textEdit)
这里咱们建立了一个TextEdit控件。咱们将它设置为QMainWindow的中心控件。中心控件会占用QMainWindow的全部剩余空间。
在这部分教程中咱们使用了菜单、工具栏、状态栏和主程序窗体。