簡體   English   中英

綁定變量PyQt單選按鈕

[英]Binding Variable PyQt Radio Buttons

我正在使用PyQt4,並希望設置一個單選按鈕列表,這些單選按鈕根據選擇內容設置變量值。 顯然,我可以使用大量的IF語句(10個單選按鈕)來完成此操作,但我認為必須有一種優雅的方法。

要在psuedocode中進行澄清:

radiobutton1 = 52
radiobutton2 = 31
radiobutton3 = 773

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

試試這個或類似的東西,遍歷您的小部件集:

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

希望能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM