python - 如何使工具栏中的工具按钮一次只能选择一个?

标签 python pyqt pyqt5 qtoolbar

我通过 toolbar.addAction()QToolbar 中有五个工具按钮,如果我选择其中一个,我想让其他按钮看起来不被选中。

示例:如果我选择“A”按钮并且程序现在处于“A”模式,则其他程序将无法运行。

最佳答案

您必须使用 QActionGroup,还必须激活 QAction 的 checkable 属性:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets


class ToolDemo(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(ToolDemo, self).__init__(parent)
        self.setWindowTitle("toolbar demo")

        toolbarBox = QtWidgets.QToolBar(self)
        toolbarBox.setFixedWidth(180)

        self.addToolBar(QtCore.Qt.RightToolBarArea, toolbarBox)

        vscode_action = QtWidgets.QAction("VSCode", self, checkable=True)
        ptt_action = QtWidgets.QAction("Ppt", self, checkable=True)
        word_action = QtWidgets.QAction("Word", self, checkable=True)
        excel_action = QtWidgets.QAction("Excel", self, checkable=True)
        other_action = QtWidgets.QAction("other", self, checkable=True)

        group = QtWidgets.QActionGroup(self, exclusive=True)

        for action in (
            vscode_action,
            ptt_action,
            word_action,
            excel_action,
            other_action,
        ):
            toolbarBox.addAction(action)
            group.addAction(action)


def main():
    app = QtWidgets.QApplication(sys.argv)
    ex = ToolDemo()
    ex.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

更新:

如果你想使用 QCheckBox、QRadioButton、QPushButton 和 QToolButton 等 QToolBar 中独有的按钮,那么你必须使用 QButtonGroup:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets


class ToolDemo(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(ToolDemo, self).__init__(parent)
        self.setWindowTitle("toolbar demo")

        toolbarBox = QtWidgets.QToolBar(self)
        toolbarBox.setFixedWidth(180)

        self.addToolBar(QtCore.Qt.RightToolBarArea, toolbarBox)

        vscode_button = QtWidgets.QCheckBox(self, text="VSCode", checkable=True)
        ptt_button = QtWidgets.QCheckBox(self, text="Ppt", checkable=True)
        word_button = QtWidgets.QRadioButton(self, text="Word", checkable=True)
        excel_button = QtWidgets.QPushButton(self, text="Excel", checkable=True)
        other_button = QtWidgets.QToolButton(self, text="other", checkable=True)

        group = QtWidgets.QButtonGroup(self, exclusive=True)

        for button in (
            vscode_button,
            ptt_button,
            word_button,
            excel_button,
            other_button,
        ):
            toolbarBox.addWidget(button)
            group.addButton(button)


def main():
    app = QtWidgets.QApplication(sys.argv)
    ex = ToolDemo()
    ex.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

关于python - 如何使工具栏中的工具按钮一次只能选择一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56414345/

相关文章:

python - 使用 QXmlStreamReader 的 XML 解析不返回所有元素

python - 如何在 Python 3 中将 QImage(QPixmap) 转换为 PIL Image?

python - PyQt5错误消息: 'bool' object has no attribute 'layout_base'

Python/Pygame - 如何将不同的透明度传输到不可见的表面上

Python Scikit 学习 : Multilabel Classification ValueError: could not convert string to float:

python - 为什么继承另一个类的类不会产生与 'another class' 相同的结果?

python - PyQt5:使用 QTableView.setSortingEnabled 和 QSortFilterProxyModel 时的幻像列

python - 使 : *** No rule to make target 'qt5py3' . 停止

Python:安装 Anaconda 后找不到模块

python - 使用 Whoosh 生成搜索词建议?