2013-07-01 1 views

답변

2

그룹 상자의 모든 라디오 버튼을 반복하고 각 라디오 박스의 isChecked() 속성을 확인해야합니다.

예 :

참조
radio1 = QtGui.QRadioButton("button 1") 
radio2 = QtGui.QRadioButton("button 2") 
radio3 = QtGui.QRadioButton("button 3") 

for i in range(1,4): 
    buttonname = "radio" + str(i) 
    if buttonname.isChecked(): 
     print buttonname + "is Checked" 

이 확인은 http://pyqt.sourceforge.net/Docs/PyQt4/qradiobutton.html

+0

위대한 솔루션입니다. 하지만 얼마나 많은 라디오 버튼이 있는지 모를 경우 어떻게 바꿀 수 있습니까? –

+0

try와 catch를 사용하여 0에서 큰 숫자의 반복을 수행하면 확인란을 선택하면 루프가 중단되고 확인란이없는 경우 예외가 catch됩니다. – scottydelta

1
def izle(self): 
     radios=["radio1","radio2","radio3","radio4"] 

     for i in range(0,4): 
      selected_radio = self.ui.findChild(QtGui.QRadioButton, self.radios[i]) 
      if selected_radio.isChecked(): 
       print selected_radio.objectName() + "is Checked" 
2

:

import sys 
from PyQt4.QtGui import * 
from PyQt4.QtCore import * 

class MoodExample(QGroupBox): 

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

     # Create an array of radio buttons 
     moods = [QRadioButton("Happy"), QRadioButton("Sad"), QRadioButton("Angry")] 

     # Set a radio button to be checked by default 
     moods[0].setChecked(True) 

     # Radio buttons usually are in a vertical layout 
     button_layout = QVBoxLayout() 

     # Create a button group for radio buttons 
     self.mood_button_group = QButtonGroup() 

     for i in xrange(len(moods)): 
      # Add each radio button to the button layout 
      button_layout.addWidget(moods[i]) 
      # Add each radio button to the button group & give it an ID of i 
      self.mood_button_group.addButton(moods[i], i) 
      # Connect each radio button to a method to run when it's clicked 
      self.connect(moods[i], SIGNAL("clicked()"), self.radio_button_clicked) 

     # Set the layout of the group box to the button layout 
     self.setLayout(button_layout) 

    #Print out the ID & text of the checked radio button 
    def radio_button_clicked(self): 
     print(self.mood_button_group.checkedId()) 
     print(self.mood_button_group.checkedButton().text()) 

app = QApplication(sys.argv) 
mood_example = MoodExample() 
mood_example.show() 
sys.exit(app.exec_()) 

나는 더 많은 정보를 발견했다.

indexOfChecked = [self.ButtonGroup.buttons()[x].isChecked() for x in range(len(self.ButtonGroup.buttons()))].index(True) 
+0

왜 간단하지 :'self.ButtonGroup.checkedId()'? – ekhumoro

+0

여러 버튼 그룹이있을 때 어떻게 든 나에게 효과가 없었습니다. -4, -3 등의 항목을 계속 사용했습니다. ( – Oxymoron88

+0

버튼을 추가 할 때 ID를 설정하지 않으면 발생합니다. 이렇게 추가하십시오 :'self.ButtonGroup.addButton (button, index)' – ekhumoro

관련 문제