2014-09-12 63 views
0

我正在使用Python 2.7.6你可以更新tkinter中單選按鈕的文本和值嗎?

我剛剛開始使用Tkinter,最近我正在將一個命令提示符測驗轉換成一個GUI測驗。到目前爲止,除了我使用的單選按鈕外,我已經取得了成功。我可以創建一組單選按鈕並在需要時提取它們的值,但我不確定在單擊「下一步」按鈕後如何更新單選按鈕的文本和值。

這不是我的整個代碼,因爲它超過800行,但這裏是我創建單選按鈕和功能def Guide():的淡化版本,我想更新單選按鈕。

假設Guide_list[Current].R1Guide_list[Current].V1分別檢索正確的字符串和整數。

「當前」是我的櫃檯。

from Tkinter import * 
app=Tk() 

Current=0 
R=IntVar() 
R.set(None) 
Resp1=Radiobutton(app, text=Guide_list[Current].R1, value=Guide_list[Current].V1,variable=R).pack(anchor=W) 
Resp2=Radiobutton(app, text=Guide_list[Current].R2, value=Guide_list[Current].V2,variable=R).pack(anchor=W) 
Resp3=Radiobutton(app, text=Guide_list[Current].R3, value=Guide_list[Current].V3,variable=R).pack(anchor=W) 
Resp4=Radiobutton(app, text=Guide_list[Current].R4, value=Guide_list[Current].V4,variable=R).pack(anchor=W) 
Resp5=Radiobutton(app, text=Guide_list[Current].R5, value=Guide_list[Current].V5,variable=R).pack(anchor=W) 
Resp6=Radiobutton(app, text="N/A", value="N/A",variable=R).pack(anchor=W) 
def Guide(): 
    global Current 
    Current=Current+1 
    ### Insert code to update the radio buttons ### 

button2=Button(app,text="Next", width=15,command=Guide) 
button2.pack(side='bottom') 

app.mainloop() 
+1

嘿,我可以問你是否使用任何教程學習Tkinter?我看到很多人使用相同的「任務+包裝反模式」,我試圖追蹤他們從哪裏得到它。 – Kevin 2014-09-20 15:17:40

+0

我正在使用我在Youtube上找到的各種教程。 – 2014-09-23 14:58:41

回答

1

您可以使用config方法修改控件的屬性。

def Guide(): 
     global Current 
     Current=Current+1 
     Resp1.config(text="Hello") 

但是,如果你仍然有一個你想要配置的小部件的現有引用,你只能這樣做。正如你的代碼現在,你沒有這個; Resp1到Resp6都是None,因爲你指向的是.pack的返回值,而不是實際的Radibuttons。另見Why is None returned instead of tkinter.Entry object。你需要打包在一個單獨的行。

Resp1=Radiobutton(app, text=Guide_list[Current].R1, value=Guide_list[Current].V1,variable=R) 
Resp2=Radiobutton(app, text=Guide_list[Current].R2, value=Guide_list[Current].V2,variable=R) 
Resp3=Radiobutton(app, text=Guide_list[Current].R3, value=Guide_list[Current].V3,variable=R) 
Resp4=Radiobutton(app, text=Guide_list[Current].R4, value=Guide_list[Current].V4,variable=R) 
Resp5=Radiobutton(app, text=Guide_list[Current].R5, value=Guide_list[Current].V5,variable=R) 
Resp6=Radiobutton(app, text="N/A", value="N/A",variable=R) 

Resp1.pack(anchor=W) 
Resp2.pack(anchor=W) 
Resp3.pack(anchor=W) 
Resp4.pack(anchor=W) 
Resp5.pack(anchor=W) 
Resp6.pack(anchor=W)