PyQt5基础学习-QMainWindow().addToolBar(工具栏) 1..addAction(添加动作) 2..setToolButtonStyle(添加图标和文字的位置关系) 3..actionTriggered.connect(绑定事件方法)

构造工具栏, 然后根据工具栏绑定事件,同时将文字放在图标的下方

"""
创建和使用工具栏

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

工具栏按钮有3种显示状态

1.只显示图标
2.只显示文本
3.只显示图标和文本
"""

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

class Toolbar(QMainWindow):
    def __init__(self):
        super(Toolbar, self).__init__()
        self.initUI()
    def initUI(self):
        self.setWindowTitle("工具栏例子")
        self.resize(300, 200)

        tb1 = self.addToolBar("File")

        new = QAction(QIcon("D:\PyQt5_Study\Action Pack Win\Icons\Ares.ico"), "new", self)
        tb1.addAction(new)

        open = QAction(QIcon("D:\PyQt5_Study\Action Pack Win\Icons\Argo.ico"), "open", self)
        tb1.addAction(open)

        save = QAction(QIcon("D:\PyQt5_Study\Action Pack Win\Icons\Autolycus.ico"), "save", self)
        tb1.addAction(save)

        tb2 = self.addToolBar("File1")
        new1 = QAction(QIcon("D:\PyQt5_Study\Action Pack Win\Icons\Callisto.ico"), "new", self)
        tb2.addAction(new1)

        tb2.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)

        tb1.actionTriggered.connect(self.toolbtnpressed)

        tb2.actionTriggered.connect(self.toolbtnpressed)

    def toolbtnpressed(self, a):
        print("按下的工具栏按钮是", a.text())

if __name__ == "__main__":
    app = QApplication(sys.argv)

    main = Toolbar()
    main.show()

    sys.exit(app.exec_())

 

posted @ 2022-02-01 15:47  c语言我的最爱  阅读(787)  评论(0编辑  收藏  举报