在PyQt5中,可以通过使用QToolBar
和QToolButton
来创建工具栏和工具栏按钮。要在点击工具栏按钮时显示下拉菜单,你可以将QToolButton
的下拉菜单设置为QMenu
。下面是一个简单的示例代码:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QToolBar, QAction, QMenu, QToolButton,
QLabel
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 创建工具栏
toolbar = self.addToolBar("Toolbar")
# 创建工具栏按钮
tool_button = QToolButton()
tool_button.setText("菜单")
tool_button.setPopupMode(QToolButton.MenuButtonPopup)