如何从pyqt中的groupbox获取已检查的radiobutton

use*_*145 2 pyqt4

我有一个带有一些radiobuttons的groupbox.我如何知道哪一个被检查.

Ale*_*lex 7

另一种方法是使用按钮组.例如:

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_())
Run Code Online (Sandbox Code Playgroud)

我发现了更多信息:

http://codeprogress.com/python/libraries/pyqt/showPyQTExample.php?index=387&key=QButtonGroupClick

http://www.pythonschool.net/pyqt/radio-button-widget/


sco*_*lta 4

您将需要遍历组框中的所有单选按钮并检查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"
Run Code Online (Sandbox Code Playgroud)

作为参考,请检查http://pyqt.sourceforge.net/Docs/PyQt4/qradiobutton.html