PyQt5教程(二)——菜单与工具栏

本文详细介绍了PyQt5中的主窗体组件,包括状态栏、菜单栏和工具栏的创建与使用方法。通过具体示例展示了如何在Python应用中集成这些组件,实现传统GUI应用程序的基本功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

主窗体

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创建状态栏控件。

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

我们需要调用QtGui.QMainWindowstatusBar()方法来创建状态栏。第一次调用该方法会创建一个状态栏对象,之后的调用都会返回这个状态栏对象。showMessage()会将消息展示在状态栏。

菜单栏

菜单栏是GUI程序的标配。它是一组位于不同菜单内的命令集。(Mac系统会以不同的方式处理菜单栏,但添加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_())  

在这个例子中我们创建了一个带有一个菜单的菜单栏。这个菜单中只有一个动作,当触发后会使程序停止。这里也创建了状态栏。可以使用Ctrl+Q快捷键触发这个动作。

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

QAction是菜单栏、工具栏或自定义快捷键中可以执行的动作的抽象表示。上面这三行代码创建了一个带有特定图标与‘Exit’标签的动作,而且还为这个动作定义了一个快捷键。第三个代码为这个动作设置了状态提示,当鼠标悬停在这个菜单项上时状态提示会显示在状态栏。

exitAction.triggered.connect(qApp.quit)

当点击这个动作时会发出triggered信号。这个信号连接到了QApplicationquit()方法。从而使程序停止。

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.QMainWindowquit()方法与triggered信号相连。

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

这里我们创建了一个工具栏并在其中添加了一个QAction对象。

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)

这里我们创建了一个TextEdit控件。我们将它设置为QMainWindow的中心控件。中心控件会占用QMainWindow的所有剩余空间。

Main Window

在这部分教程中我们使用了菜单、工具栏、状态栏和主程序窗体。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值