当我运行PyQt5程序时,为什么第一个按钮是蓝色的?

时间:2019-06-06 13:03:00

标签: python pyqt5

我使用三个按钮创建了一个简单的布局,但是我创建的第一个按钮在Mac(运行Mojave)上始终为蓝色

class Window(QDialog):

    def __init__(self):
        super().__init__()

        self.title = "Button Groups PyQt5"
        self.top = 100
        self.left = 100
        self.width = 500
        self.height = 100

        self.initWindow()

    def initWindow(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        hbox = QHBoxLayout()

        self.buttonGroup = QButtonGroup()


       # This button always appears to be blue
        button = QPushButton("Python")
        self.buttonGroup.addButton(button, 1)
        hbox.addWidget(button)

        button1 = QPushButton("Java")
        self.buttonGroup.addButton(button1, 2)
        hbox.addWidget(button1)

        button2 = QPushButton("C++")
        self.buttonGroup.addButton(button2, 3)
        hbox.addWidget(button2)

        self.setLayout(hbox)

        self.show()

我希望所有按钮都使用相同的颜色,但是第一个按钮始终是蓝色,例如:https://cdn1.imggmi.com/uploads/2019/6/6/6949e107362afe4376f6386052c9a5b5-full.png

1 个答案:

答案 0 :(得分:1)

该按钮为蓝色是因为因为它是第一项,所以它正在获得焦点。如果您不希望任何按钮获得焦点,则可以添加以下代码:

button.setFocusPolicy(QtCore.Qt.NoFocus)
button1.setFocusPolicy(QtCore.Qt.NoFocus)
button2.setFocusPolicy(QtCore.Qt.NoFocus)