原文: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的全部剩餘空間。
在這部分教程中咱們使用了菜單、工具欄、狀態欄和主程序窗體。