• PyQt5中为窗口添加菜单工具栏状态栏


    QtDesigner中添加菜单

    移除菜单栏

    在这里插入图片描述

    添加菜单栏

    • ToolBar也类似。
      在这里插入图片描述
      在这里插入图片描述

    代码中添加菜单

    • 添加菜单选项用addMenu,添加真正要执行动作的项用addAction。
    • 当要添加二级目录时,使用addMenu。
    • 当要进行更详细的设定如快捷键等,创建QAction对象,设置好后,再将其利用addAction加入到菜单中。
    class Menu(QMainWindow):
        def __init__(self):
            super(Menu, self).__init__()
            self.init_ui()
    
        def init_ui(self):
            menu1 = self.menuBar()
            file = menu1.addMenu("File")
            load = file.addMenu("Load")
            load.addAction("load_ok")
            file.addAction("New")
            file.addAction("Open")
            save = QAction("Save", self)
            save.setShortcut("CTRL + S")
            file.addAction(save)
            save.triggered.connect(self.process)
    
        def process(self):
            print(self.sender().text())
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        ui = Menu()
        ui.show()
        sys.exit(app.exec())
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    QtDesigner中添加工具栏

    • 工具栏中的东西和菜单栏是共用同一套东西的。
    • 从ActionEditor里面选中直接拖动到工具栏上。
    • 可以在Icon中指定显示图片。在这里插入图片描述
      在这里插入图片描述

    代码中添加工具栏

    • 首先使用addToolBar创建工具栏对象,可以有多个。
    • 然后设置好QAction对象,再使用toolbar的addAction方法加入QAction对象。
    • 工具栏的图标可以有对应的显示信息,或是以提示信息的形式存在,默认。或是存在于下方右方等,或是直接不显示。可以通过toolbar的setToolButtonStyle方法进行改变。
    • 当需要不同的图标显示信息不同时,可以创建多个工具栏对象。
    import sys
    from PyQt5.QtWidgets import *
    from PyQt5.QtGui import *
    from PyQt5.QtCore import *
    
    
    class Toolbar(QMainWindow):
        def __init__(self):
            super().__init__()
            self.init_ui()
    
        def init_ui(self):
            tool_bar1 = self.addToolBar("File")
            new_file = QAction(QIcon("dog.png"), "new file", self)
            tool_bar1.addAction(new_file)
            new_file.triggered.connect(self.process)
    
            open_file = QAction(QIcon("tiger.png"), "open file", self)      # 显示信息new_file只是作为提示信息显示
            tool_bar1.addAction(open_file)
            open_file.triggered.connect(self.process)
            tool_bar1.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)       # 显示在右侧
            tool_bar1.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)        # 显示在下方
    
            # 当需要不同的图标显示不同的状态时,需要设置不同的toolbar
            tool_bar2 = self.addToolBar("File")
            new_file = QAction(QIcon("dog.png"), "new file", self)
            tool_bar2.addAction(new_file)
            new_file.triggered.connect(self.process)
            tool_bar2.setToolButtonStyle(Qt.ToolButtonIconOnly)
    
        def process(self):
            print(self.sender().text())
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        ui = Toolbar()
        ui.show()
        sys.exit(app.exec())
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    QtDesigner中添加状态栏

    • 同添加菜单栏和工具栏的方法。

    代码中添加状态栏

    import sys
    from PyQt5.QtWidgets import *
    
    
    class Statusbar(QMainWindow):
        def __init__(self):
            super().__init__()
            self.init_ui()
    
        def init_ui(self):
            self.resize(500, 500)
            menu_bar = self.menuBar()
            file = menu_bar.addMenu("File")
            new_file = file.addAction("new")
            new_file.triggered.connect(self.process)
            self.status_bar = QStatusBar()
            self.setStatusBar(self.status_bar)
    
        def process(self):
            print("aaaa")
            # if q.text == "new":
            self.status_bar.showMessage("you want to create a new file!", 3000)
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        ui = Statusbar()
        ui.show()
        sys.exit(app.exec())
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
  • 相关阅读:
    Yolov3-v5正负样本匹配机制
    CPDA|运营人如何从负基础学会数据分析(SQL)
    论文投稿指南——中文核心期刊推荐(电子、通信技术)
    【管理运筹学】第 7 章 | 图与网络分析(4,最大流问题)
    线性代数与解析几何——Part3 线性空间 & 线性变换
    网页编辑软件Whisk mac中文版功能介绍
    在云计算环境中,如何利用 AI 改进云计算系统和数据库系统性能
    金仓数据库 KingbaseES PL/SQL 过程语言参考手册(17. B PL/SQL名称解析)
    SpringBoot 下载 docx 文档
    css 基础语法
  • 原文地址:https://blog.csdn.net/weixin_48668114/article/details/126814751