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

本文介绍了如何在Qt中使用QtDesigner和代码分别添加及管理窗口的菜单、工具栏和状态栏。通过实例展示了如何创建菜单项、设置快捷键、添加工具栏图标以及更新状态栏消息。内容包括菜单的添加与移除、工具栏的图标设置和状态栏的使用方法。

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

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())

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())

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())
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值