Как заставить кнопки инструментов на панели инструментов выбирать только по одной за раз? - PullRequest
1 голос
/ 02 июня 2019

У меня есть пять кнопок инструментов в QToolbar через toolbar.addAction(), и если я выберу одну из них, я бы хотел, чтобы другие не выглядели проверенными.

Пример. Если я выберу кнопку «А», и программа перейдет в режим «А», другие не смогут работать.

1 Ответ

2 голосов
/ 02 июня 2019

Вы должны использовать QActionGroup, вы также должны активировать свойство checkable QAction:

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()
...