from PySide6.QtCore import Qt
from PySide6.QtWidgets import QApplication, QLabel, QMainWindow, QPushButton, QToolBar
class MyToolBar(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('ToolBar Demo')
self.label = QLabel('Hello, ToolBar')
self.label.setAlignment(Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter)
self.setCentralWidget(self.label)
self.tool_bar = QToolBar('MyToolBar')
self.tool_bar.setToolTip('this is my tool-bar')
self.tool_bar.addWidget(QPushButton('ToolButton'))
self.tool_bar.toggleViewAction().setEnabled(False) # 当设置为 False 时,鼠标右键不能关闭工具栏
self.addToolBar(self.tool_bar)
if __name__ == '__main__':
app = QApplication()
ins = MyToolBar()
ins.show()
app.exec()
运行效果
关闭工具栏
运行效果
使用 QStatusBar 显示状态
示例代码
代码语言:python代码运行次数:0复制
from datetime import datetime
from PySide6.QtCore import Qt
from PySide6.QtGui import QAction
from PySide6.QtWidgets import QApplication, QLabel, QMainWindow, QStatusBar, QToolBar
def action_button_toggled(state: bool):
print('action button toggled to:{} @{}'.format(state, datetime.now().isoformat(sep = ' ')))
class MyToolBar(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('ToolBar Demo')
self.label = QLabel('Hello, ToolBar')
self.label.setAlignment(Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter)
self.setCentralWidget(self.label)
self.tool_bar = QToolBar('MyToolBar')
self.tool_bar.setToolTip('this is my tool-bar')
self.tool_bar.toggleViewAction().setEnabled(False) # 当设置为 False 时,鼠标右键不能关闭工具栏
# 创建一个 QAction,设置父对象为当前窗口
# 当parent控件被销毁时,它的子控件也会被自动销毁
# 这有助于防止内存泄漏和资源管理问题
self.action_button = QAction('ToolBarActionButton', self)
self.action_button.setToolTip('this is my toolbar action button')
self.action_button.setStatusTip('action button status tip')
self.action_button.setCheckable(True)
self.action_button.triggered.connect(self.action_button_triggered)
self.action_button.toggled.connect(action_button_toggled)
self.tool_bar.addAction(self.action_button)
# 创建一个 QStatusBar,设置父对象为当前窗口
# 当parent控件被销毁时,它的子控件也会被自动销毁
# 这有助于防止内存泄漏和资源管理问题
self.status_bar = QStatusBar(self)
self.setStatusBar(self.status_bar)
self.addToolBar(self.tool_bar)
def action_button_triggered(self):
# ISO 8601 格式时间
self.status_bar.showMessage(
f"action-button triggered@{datetime.now().isoformat(sep = ' ')}")
if __name__ == '__main__':
app = QApplication()
ins = MyToolBar()
ins.show()
app.exec()
运行效果
设置工具栏图标
示例代码
代码语言:python代码运行次数:0复制
from PySide6.QtCore import QSize, Qt
from PySide6.QtGui import QAction, QIcon
from PySide6.QtWidgets import QApplication, QLabel, QMainWindow, QToolBar
class MyToolBar(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('ToolBar Demo')
self.label = QLabel('Hello, ToolBar')
self.label.setAlignment(Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter)
self.setCentralWidget(self.label)
self.tool_bar = QToolBar('MyToolBar')
self.tool_bar.setToolTip('this is my tool-bar')
self.tool_bar.toggleViewAction().setEnabled(False) # 当设置为 False 时,鼠标右键不能关闭工具栏
self.tool_bar.setIconSize(QSize(64, 64))
# Qt uses your operating system default settings to determine
# whether to show an icon, text or an icon and text in the toolbar
# But you can override this by using.setToolButtonStyle
# 此工具栏显式指定样式
self.tool_bar.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextUnderIcon)
# 创建一个 QAction,设置父对象为当前窗口
# 当parent控件被销毁时,它的子控件也会被自动销毁
# 这有助于防止内存泄漏和资源管理问题
self.action_button = QAction(
QIcon('xxxxxxx/xxxxxxx/xxxxxxx.png'),
'ToolBarActionButton',
self)
self.action_button.setToolTip('this is my toolbar action button')
self.action_button.setStatusTip('action button status tip')
self.action_button.setCheckable(True)
self.tool_bar.addAction(self.action_button)
self.addToolBar(self.tool_bar)
# 此工具栏不显式指定样式
self.tool_bar_2 = QToolBar('AnotherToolBar')
self.tool_bar_2.toggleViewAction().setEnabled(False)
self.tool_bar_2.setIconSize(QSize(64, 64))
self.action_button_2 = QAction(
QIcon('xxxxxxx/xxxxxxx/xxxxxxx.png'),
'AnotherToolBarActionButton',
self)
self.action_button_2.setToolTip('this is my ANOTHER toolbar action button')
self.action_button_2.setStatusTip('action button status tip')
self.action_button_2.setCheckable(True)
self.tool_bar_2.addAction(self.action_button_2)
self.addToolBar(self.tool_bar_2)
# 如果工具栏自身没有显式指定,则使用这种样式
self.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextBesideIcon)
if __name__ == '__main__':
app = QApplication()
ins = MyToolBar()
ins.show()
app.exec()