PyQt5教程——菜單和工具欄(3)


PyQt5中的菜單和工具欄

在這部分的PyQt5教程中,我們將創建菜單和工具欄。菜單式位於菜單欄的一組命令操作。工具欄是應用窗體中由按鈕和一些常規命令操作組成的組件。

主窗口

QMainWindow類提供了一個應用主窗口。默認創建一個擁有狀態欄、工具欄和菜單欄的經典應用窗口骨架。

狀態欄

狀態欄是用來顯示狀態信息的組件。

#!/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組件幫助創建完成(依賴於QMainWindow組件)。

self.statusBar().showMessage('Ready')

為了得到狀態欄,我們調用了QtGui.QMainWindow類的statusBar()方法。第一次調用這個方法創建了一個狀態欄。隨后方法返回狀態欄對象。然后用showMessage()方法在狀態欄上顯示一些信息。

菜單欄

菜單欄是GUI應用的常規組成部分。是位於各種菜單中的一組命令操作(Mac OS 對待菜單欄有些不同。為了獲得全平台一致的效果,我們可以在代碼中加入一行:menubar.setNativeMenuBar(False))。

#!/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_()) 

 

在上面的例子中,我們創建了有一個菜單項的菜單欄。這個菜單項包含一個選中后中斷應用的動作。

exitAction = QAction(QIcon('exit.png'), '&Exit', self)        
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('Exit application')

QAction是一個用於菜單欄、工具欄或自定義快捷鍵的抽象動作行為。在上面的三行中,我們創建了一個有指定圖標和文本為'Exit'的標簽。另外,還為這個動作定義了一個快捷鍵。第三行創建一個當我們鼠標浮於菜單項之上就會顯示的一個狀態提示。

exitAction.triggered.connect(qApp.quit)

當我們選中特定的動作,一個觸發信號會被發射。信號連接到QApplication組件的quit()方法。這樣就中斷了應用。

menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(exitAction)

menuBar()方法創建了一個菜單欄。我們創建一個file菜單,然后將退出動作添加到file菜單中。

工具欄

菜單可以集成所有命令,這樣我們可以在應用中使用這些被集成的命令。工具欄提供了一個快速訪問常用命令的方式。

#!/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)

 

我們創建了一個動作對象,和之前菜單欄中的部分代碼相似。這個動作有一個標簽,圖標和快捷鍵。並且將QtGui.QMainWindow的quit()方法連接到了觸發信號上。

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

 

這里我們創建了一個工具欄,並且在其中插入一個動作對象。

ToolbarFigure: Toolbar

將幾個組件放在一起使用

在上面的例子中,我們創建了菜單欄、工具欄和狀態欄。下面我們將創建一個中心組件。

#!/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)

 

在這里我們創建了一個文本編輯框組件。我們將它設置成QMainWindow的中心組件。中心組件占據了所有剩下的空間。

Main windowFigure: Main window

在這個部分的PyQt5教程中,我們使用了菜單、工具欄、狀態欄和一個應用主窗口。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM