QToolBar工具栏作用
QToolBar控件是由文本按钮、图标或其他小控件按钮组成的可移动面板,通常位于菜单栏下方。
QToolBar常用方法
每当单击工具栏中的按钮时,都将发射actionTriggered信号。
功能结果显示
代码示例
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class ToolBarDemo( QMainWindow ):
def __init__(self, parent=None):
super(ToolBarDemo, self).__init__(parent)
self.setWindowTitle("toolbar 例子")
self.resize(300, 200)
layout = QVBoxLayout()
# 在工具栏区域添加文件工具栏
tb = self.addToolBar("File")
new = QAction(QIcon("./images/new.png"),"new",self)
tb.addAction(new)
open = QAction(QIcon("./images/open.png"),"open",self)
tb.addAction(open)
save = QAction(QIcon("./images/save.png"),"save",self)
tb.addAction(save)
tb.actionTriggered[QAction].connect(self.toolbtnpressed)
self.setLayout(layout)
def toolbtnpressed(self,a):
print("pressed tool button is",a.text() )
if __name__ == '__main__':
app = QApplication(sys.argv)
demo = ToolBarDemo()
demo.show()
sys.exit(app.exec_())