【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之后消失


总结

这篇文章依旧没有总结

相关推荐
云空20 小时前
《PyQt6-3D应用开发技术文档》
3d·pyqt
sword devil9001 天前
PYQT实战:无刷电机模拟(只是模拟,没有写接口接收外部数据)
pyqt
sword devil9004 天前
PYQT实战:智能家居中控
python·智能家居·pyqt
OICQQ676580089 天前
创建一个基于YOLOv8+PyQt界面的驾驶员疲劳驾驶检测系统 实现对驾驶员疲劳状态的打哈欠检测,头部下垂 疲劳眼睛检测识别
yolo·pyqt·疲劳驾驶·检测识别·驾驶员检测·打哈欠检测·眼睛疲劳
小灰灰搞电子18 天前
Qt PyQt与PySide技术-C++库的Python绑定
c++·qt·pyqt
越甲八千19 天前
pyqt 简单条码系统
数据库·microsoft·pyqt
重生之我在厦门做测试20 天前
基于pyqt开发串口和对应映射值(path)的显示工具
pyqt
hvinsion23 天前
【开源解析】基于Python+Qt打造智能应用时长统计工具 - 你的数字生活分析师
python·pyqt·开源软件·应用时长统计
毕设做完了吗?1 个月前
基于YOLOv8+Deepface的人脸检测与识别系统
python·yolo·毕业设计·pyqt
懷淰メ1 个月前
python3GUI--基于PyQt5+DeepSort+YOLOv8智能人员入侵检测系统(详细图文介绍)
开发语言·深度学习·yolo·目标检测·pyqt·课程设计·deepsort