【问题标题】:Binding Variable PyQt Radio Buttons绑定变量 PyQt 单选按钮
【发布时间】:2014-06-21 18:38:58
【问题描述】:

我正在使用 PyQt4 并希望设置一个单选按钮列表,这些单选按钮设置一个取决于选择的变量值。显然我可以用大量的 IF 语句(10 个单选按钮)来做到这一点,但我认为必须有一种优雅的方式来做到这一点。

在伪代码中澄清:

radiobutton1 = 52
radiobutton2 = 31
radiobutton3 = 773

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

【问题讨论】:

    标签: python pyqt4 qtgui


    【解决方案1】:

    试试这个或类似的东西,遍历你的小部件集:

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

    您还可以在列表中设置单选按钮集并进行迭代 通过这个对象列表而不是使用字符串操作。 示例:

    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
    

    希望对您有所帮助。

    【讨论】:

    • 谢谢!这个更好!效果很好!
    猜你喜欢
    • 1970-01-01
    • 2011-01-20
    • 2012-02-12
    • 2015-10-31
    • 2011-10-06
    • 1970-01-01
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多