简体   繁体   English

绑定变量PyQt单选按钮

[英]Binding Variable PyQt Radio Buttons

I am using PyQt4 and want to set up a list of radio buttons that set a variable value dependent on the selection. 我正在使用PyQt4,并希望设置一个单选按钮列表,这些单选按钮根据选择内容设置变量值。 Obviously I can do it with a ton of IF statements (10 radio buttons) but I figured there must be an elegant way to do it. 显然,我可以使用大量的IF语句(10个单选按钮)来完成此操作,但我认为必须有一种优雅的方法。

To clarify in psuedocode: 要在psuedocode中进行澄清:

radiobutton1 = 52
radiobutton2 = 31
radiobutton3 = 773

if radiobutton1 x = 52
if radiobutton2 x = 31
if radiobutton3 x = 773

try this or something similar, iterating through your widget set: 试试这个或类似的东西,遍历您的小部件集:

for i in range(1,4):
    widgetname = 'radiobutton' + str(i)
    if widgetname.isChecked():
    return widgetname.value

You could also set up the set of radio buttons in a list and iterate through this list of objects rather than using string manipulations. 您还可以在列表中设置单选按钮集,并遍历此对象列表,而不是使用字符串操作。 Example: 例:

rb_list = [ rb1, rb2, rb3 ] # where items are your radio buttons
#these can be appended to the list when the objects are created
def rb_check(rbl=rb_list):
    for rb in rbl:
        if rb.isChecked():
            print("RadioButton", rb, "is checked.") # or return value

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM