我有一个GroupBox,里面有一些RadioButton。如何知道哪个被选中了。
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_())
我在这里找到了更多信息:
http://codeprogress.com/python/libraries/pyqt/showPyQTExample.php?index=387&key=QButtonGroupClick
您需要迭代组合框中的所有单选按钮,并检查每个单选框的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了解相关的技术内容。
isChecked
。您可以通过首先评估buttonname
来使其工作:buttonname = eval("radio" + str(i))
。然而,我不建议这样做,最好将单选按钮放在一个列表中,并对该列表进行迭代。 - Bruno Vermeulen我通过使用索引和循环的组合成功解决了这个问题。
indexOfChecked = [self.ButtonGroup.buttons()[x].isChecked() for x in range(len(self.ButtonGroup.buttons()))].index(True)
self.ButtonGroup.checkedId()
呢? - ekhumoroself.ButtonGroup.addButton(button, index)
。 - ekhumoro 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"
你可以从所需的父级获取类的所有子对象。就像这样:
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setupUi(self)
self.show()
print(self.getCheckedRbName(self.gbRadioButtonsGroup))
def getCheckedRbName(self, rbParent: QWidget) -> str:
for rb in rbParent.findChildren(QRadioButton):
if rb.isChecked():
return rb.objectName()
提供给你
#> 'rbThirdOption' is checked
由于scottydelta的答案在PyQt6中不起作用,因此这里提供了另一种解决方案,方法相同但更加一致。
for radiobutton in ["button1", "button2", "button3"]:
if getattr(self, radiobutton).isChecked():
print(radiobutton, " is checked.")
# To get the radiobuttons text:
# print(getattr(self, radiobutton).text())
self.button1 = QRadioButton()
self.button2 = QRadioButton()
attr_objects = [attr_obj for attr_obj in map(lambda x: getattr(self, x), vars(self).keys()) if isinstance(attr_obj, QRadioButton)]
通过最后一行的条件语句,您可以将列表限制为单选按钮对象。
然后,您可以检查这些对象中哪些具有特定属性,例如它们是否被选中。
map(isChecked(), attr_objects)
button_group.checkedButton()
是重要的部分,以防有人不想浏览所有代码。 - Guimoute