【PyQt】18 -菜单等顶层操作

顶层界面的使用


前言

1、介绍顶层菜单栏目的使用,但没有陆续绑定槽函数。

2、工具栏

3、状态栏


一、菜单栏

1.1 代码

bash 复制代码
'''
#Author :susocool
#Creattime:2024/3/24
#FileName:42-创建和使用菜单
#Description: 

'''
import sys,math
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

class Menu( QMainWindow ):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('创建和使用菜单')
        self.resize(400,300)

        bar = self.menuBar()     # 获取菜单栏

        file = bar.addMenu('文件')
        file.addAction("新建")        # 创建动作

        save = QAction("保存",self)        # 创建动作的另一种表示
        save.setShortcut("Ctrl + S")    # 设置快捷键
        file.addAction(save)

        edit = bar.addMenu("edit")
        edit.addAction("copy")
        edit.addAction("paste")

        quit = QAction("退出",self)
        file.addAction(quit)

        save.triggered.connect(self.process)

    def process(self,a):
        # print(a.text())   # text实际上是获得 bool类型,因此不需要
        print(self.sender().text())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ui = Menu()
    ui.show()
    sys.exit(app.exec_())

1.2 运行结果



二、工具栏

工具栏默认按钮:只显示图标,将文本作为悬停提示

2.1 代码

bash 复制代码
'''
#Author :susocool
#Creattime:2024/3/24
#FileName:43-工具栏
#Description: 

'''
import sys,math
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

class Toolbar(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('创建和使用菜单栏')
        self.resize(400,200)

        toolbar = self.addToolBar("File")

        # 规范化的话,应该创建一个Icon文件夹存放图片
        new = QAction(QIcon("./添加文件.jpg"),"添加文件",self)       # 创建在当前窗口上,文字可以变成悬停提示
        toolbar.addAction(new)

        open = QAction(QIcon("./打开文件.png"),"打开文件",self)
        toolbar.addAction(open)

        save = QAction(QIcon("./保存文件.jpg"),"保存文件",self)
        toolbar.addAction(save)

        toolbar.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)

        # 槽函数
        toolbar.actionTriggered.connect(self.toolbuttonpressed)
    def toolbuttonpressed(self,a):
        print('按下的工具栏按钮是 ',a.text())


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ui = Toolbar()
    ui.show()
    sys.exit(app.exec_())

几种显示方法

bash 复制代码
        toolbar.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)

图标和文本并存,此处选择在右侧展示

在其底部

2.2 运行结果

三、状态栏

状态栏:用于显示状态信息

3.1 代码

bash 复制代码
'''
#Author :susocool
#Creattime:2024/3/24
#FileName:44-状态栏
#Description: 

'''
import sys,math
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

class Toolbar(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('创建和使用菜单栏')
        self.resize(400,200)

        bar = self.menuBar()
        file = bar.addMenu("File")  # 添加顶层菜单文件
        file.addAction("show")  # 添加动作
        file.triggered.connect(self.processTrigger) # 添加触发动作

        self.setCentralWidget(QTextEdit())	# 没实际用途的编辑界面

        self.statusBar = QStatusBar()   # 创建状态栏
        self.setStatusBar(self.statusBar)


    def processTrigger(self,q):
        if q.text() == "show":
            # 这里之前犯了个错误,不是statusBar()
            self.statusBar.showMessage(q.text() + "菜单被点击了",5000) # 显示5s之后会消失


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ui = Toolbar()
    ui.show()
    sys.exit(app.exec_())

3.2 运行结果

5s之后消失


总结

这篇文章依旧没有总结

相关推荐
深蓝海拓3 天前
基于QtPy (PySide6) 的PLC-HMI工程实战记录(五)为当前动作画面的信号连接变量
网络·笔记·python·学习·pyqt
智购科技自动售卖机厂家6 天前
2026自动售货机OTA升级系统设计:从全量升级到差分升级的带宽优化工程实践~YH
大数据·人工智能·numpy·pyqt·fastapi
【赫兹威客】浩哥6 天前
【无标题】
python·spring·django·pyqt
小灰灰搞电子8 天前
PyQt QTextBoundaryFinder类详解:精准定位文本边界
pyqt
小灰灰搞电子9 天前
PyQt 实现自己的桌面萌宠源码分享
pyqt
小灰灰搞电子12 天前
PyQt6 QCommandLineParser 类详解:命令行参数解析实战指南
pyqt·参数解析
微小冷14 天前
pyQT中的文本部件及其区别
pyqt·gui·富文本编辑器·文本输入框·文本浏览器
房开民18 天前
PyQt 翻译(国际化)极简讲解
pyqt
房开民22 天前
PyQt5 常用模块(对应Qt五大模块)
数据库·pyqt
懷淰メ25 天前
【AI赋能】基于PyQt+YOLO+DeepSeek水上漂浮物检测系统(详细介绍)
人工智能·yolo·目标检测·计算机视觉·pyqt·漂浮物·水上漂浮物