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


总结

这篇文章依旧没有总结

相关推荐
酷飞飞4 天前
PyQt 界面布局与交互组件使用指南
python·qt·交互·pyqt
qq_340474025 天前
Q3.1 PyQt 中的控件罗列
pyqt
万粉变现经纪人5 天前
如何解决pip安装报错ModuleNotFoundError: No module named ‘sympy’问题
python·beautifulsoup·pandas·scikit-learn·pyqt·pip·scipy
Goona_6 天前
PyQt数字转大写金额GUI工具开发及财务规范实现
python·小程序·交互·pyqt
小叮当⇔6 天前
PYcharm——pyqt音乐播放器
ide·pycharm·pyqt
青铜发条7 天前
【Qt】PyQt、原生QT、PySide6三者的多方面比较
开发语言·qt·pyqt
Goona_8 天前
pyqt+python之二进制生肖占卜
pyqt
大学生毕业题目10 天前
毕业项目推荐:83-基于yolov8/yolov5/yolo11的农作物杂草检测识别系统(Python+卷积神经网络)
人工智能·python·yolo·目标检测·cnn·pyqt·杂草识别
凯子坚持 c13 天前
当Python遇见高德:基于PyQt与JS API构建桌面三维地形图应用实战
javascript·python·pyqt·高德地图