2

我正在用 PyQt 编写一个向导。在第一页上,我想让用户在两个选项之间选择两次。因此,我决定制作 Buttongroups。但是虽然我将前两个单选按钮添加到第一个 Buttongroup 并将另外两个添加到第二个按钮组,但这些按钮仍然是独占的(我只能在页面上选择一个按钮)。

这是我的代码:

    # Setup UI
    layout = QtGui.QVBoxLayout()

    gBBackupFromIntExt = QtGui.QGroupBox()
    layout.addWidget(gBBackupFromIntExt)

    bGBackupFromIntExt = QtGui.QButtonGroup()

    self.rBBackupFromExt = QtGui.QRadioButton()
    bGBackupFromIntExt.addButton (self.rBBackupFromExt)
    layout.addWidget(self.rBBackupFromExt)

    self.rBBackupFromInt = QtGui.QRadioButton()
    bGBackupFromIntExt.addButton (self.rBBackupFromInt)
    layout.addWidget(self.rBBackupFromInt)

    gBBackupToIntExt = QtGui.QGroupBox()
    layout.addWidget(gBBackupToIntExt)

    bGBackupToIntExt = QtGui.QButtonGroup()

    self.rBBackupToExt = QtGui.QRadioButton()
    bGBackupToIntExt.addButton (self.rBBackupToExt)
    layout.addWidget(self.rBBackupToExt)

    self.rBBackupToInt = QtGui.QRadioButton()
    bGBackupToIntExt.addButton (self.rBBackupToInt)
    layout.addWidget(self.rBBackupToInt)

你知道我的错误在哪里,我必须改变什么吗?

4

1 回答 1

4

问题是 QButtonGroups 从未成为层次结构的一部分,因此它们没有效果。

顺便说一句:发布代码时尝试添加必要的部分以使其可运行:

from PyQt4 import QtGui
import sys

class Test(QtGui.QWidget):
    def __init__(self):
        super().__init__()
        layout = QtGui.QVBoxLayout(self)

        gBBackupFromIntExt = QtGui.QGroupBox()
        layout.addWidget(gBBackupFromIntExt)

        bGBackupFromIntExt = QtGui.QButtonGroup(self)

        self.rBBackupFromExt = QtGui.QRadioButton()
        bGBackupFromIntExt.addButton(self.rBBackupFromExt)
        layout.addWidget(self.rBBackupFromExt)

        self.rBBackupFromInt = QtGui.QRadioButton()
        bGBackupFromIntExt.addButton(self.rBBackupFromInt)
        layout.addWidget(self.rBBackupFromInt)

        gBBackupToIntExt = QtGui.QGroupBox()
        layout.addWidget(gBBackupToIntExt)

        bGBackupToIntExt = QtGui.QButtonGroup(self)

        self.rBBackupToExt = QtGui.QRadioButton()
        bGBackupToIntExt.addButton (self.rBBackupToExt)
        layout.addWidget(self.rBBackupToExt)

        self.rBBackupToInt = QtGui.QRadioButton()
        bGBackupToIntExt.addButton (self.rBBackupToInt)
        layout.addWidget(self.rBBackupToInt)

a = QtGui.QApplication(sys.argv)
t = Test()
t.show()
a.exec()

这应该可以解决问题。

于 2012-12-05T21:09:30.013 回答