【问题标题】:Single radiobutton in groupbox can be deselected可以取消选择 groupbox 中的单个单选按钮
【发布时间】:2019-06-16 02:48:02
【问题描述】:

我试图搜索相应的主题,但我找不到,所以我在这里。 我在 Python 和 PyQt4 中创建了一个应用程序,我可以在其中从文件中导入一些数据。在导入期间,我在其中动态创建了一些组框和单选按钮,以供用户显示所需的数据。由于收集到的数据,组框可能只有一个单选按钮。

问题是当用户点击这个已经选择的单选按钮时,它被取消选择(中间没有黑点)。再次点击它再次选择它......

这是一个错误还是我应该设置一个属性以使这种行为不会发生(这意味着该按钮永远不会被取消选择,因为它在 groupbox 中是单独的)?

如何防止这种行为?

示例(添加)

import sys
from PyQt4 import QtCore, QtGui

class MyApp(QtGui.QMainWindow):
    def __init__(self):
        super(MyApp, self).__init__()
        self.resize(289, 171)
        self.centralwidget = QtGui.QWidget(self)
        self.setCentralWidget(self.centralwidget)
        self.gridLayout = QtGui.QGridLayout(self.centralwidget)
        self.groupBox = QtGui.QGroupBox(self.centralwidget)
        self.groupBox.setGeometry(QtCore.QRect(0, 0, 100, 100))
        self.groupBox.setTitle("GroupBox")
        self.gridLayout.addWidget(self.groupBox, 0, 0, 1, 1)
        self.radioButton = QtGui.QRadioButton(self.groupBox)
        self.radioButton.setGeometry(QtCore.QRect(20, 60, 82, 17))
        self.radioButton.setChecked(True)
        self.radioButton.setAutoExclusive(True)
        self.radioButton.setText("RadioButton")
        self.gridLayoutRb = QtGui.QGridLayout(self.groupBox)
        self.gridLayoutRb.addWidget(self.radioButton, 0,0,1,1)
        self.show()

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    app.setStyle(QtGui.QStyleFactory.create('cleanlooks'))
    window = MyApp()
    out_msg = app.exec_()
    sys.exit(out_msg)

【问题讨论】:

  • 这当然不是错误。处理此问题的正常方法是简单地禁用该按钮。这样一来,用户就清楚按钮的状态无法更改。

标签: python python-2.7 pyqt pyqt4


【解决方案1】:

正如@ekhumoro 建议的那样,可以禁用单选按钮,因为只有一个按钮。缺点是按钮及其文本是灰色的。如果不是问题,那就是解决方案。

另一种方法是在 groupBox 中永久添加一个单选按钮,然后设置其坐标(通过将其属性更改为负值)将其放在 groupBox 之外。这只有在不使用布局时才可能

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-27
    • 2012-02-21
    • 2013-08-09
    • 2013-10-19
    • 1970-01-01
    • 1970-01-01
    • 2011-07-17
    • 2011-11-05
    相关资源
    最近更新 更多