2012-12-05 70 views
2

我正在用PyQt編寫一個嚮導。在第一頁上,我想讓用戶選擇在兩個選項之間選擇兩次。 因此我決定向Buttongroups做出決定。但是,儘管我將第一個兩個單選按鈕添加到第一個Buttongroup,其他兩個單選按鈕仍然是排他性的(我只能選擇頁面上的一個按鈕)。一個窗口中有多個QButtonGroups

這裏是我的代碼:

# 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) 

你有任何想法,是我的錯誤,我要改變什麼?

回答

2

問題是,QButtonGroups從不成爲層次結構的一部分,所以它們沒有任何效果。

BTW:發佈代碼時嘗試添加neccessary部分,使其可運行:

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

這應該解決這個問題。

+1

非常感謝您的回答以及關於未來職位的信息,現在工作正常! 對於其他人:解決方法是在ButtonGroups的括號中添加「self」(即bGBackupToIntExt = QtGui.QButtonGroup(** self **) –