2016-02-19 55 views
0

的StackOverflow,的Tkinter Checkbutton狀態更改使用字典

所以我仍然工作在這個GUI中的Tkinter和我建立了我的checkbutton字典和諸如此類的東西。實質上,我有一個主要的檢查按鈕組,然後是3個子檢查按鈕組,其狀態應根據主要檢查按鈕組中的哪個按鈕處於活動狀態進行更改。然而,我的問題是,只有子檢查按鈕組的最後一個檢查按鈕變爲活動狀態,而其上方的按鈕仍處於禁用狀態。

下面是代碼:

def enable_location_state(): 

#Retrieve values to determine whether or not the checkbutton is checked 
#0 is off, 1 is on 
    dt1 = datatype['Joint Angle'].get() 
    dt2 = datatype['Joint Acceleration'].get() 
    dt3 = datatype['Ground Reaction Force'].get() 
    dt4 = datatype['Muscle Activation'].get() 


    if dt1 == 1 or dt2 == 1: 
     ja_cb.configure(state=ACTIVE) 
    elif dt1 == 0 and dt2 == 0: 
     ja_cb.configure(state=DISABLED) 

    if dt3 == 1: 
     grf_cb.configure(state=ACTIVE) 
    elif dt3 == 0: 
     grf_cb.configure(state=DISABLED) 

    if dt4 == 1: 
     emg_cb.configure(state=ACTIVE) 
    elif dt4 == 0: 
     emg_cb.configure(state=DISABLED) 


ilabel1 = Label(root, text=' Measurement',font=("Bold",18)).grid(row=1,column=0) 

#Options for the checkbuttons 
datatype = {'Joint Angle' : 0, 
     'Joint Acceleration' : 0, 
     'Ground Reaction Force' : 0, 
     'Muscle Activation' : 0 
} 


for measure in datatype: 
    datatype[measure] = IntVar() 
    dt_cb = Checkbutton(root, text=measure, 
         variable=datatype[measure],command =  enable_location_state) 
    dt_cb.grid(column=0, sticky='W', padx=20) 



#EMG 
ilabel2 = Label(root, text='Muscle Group(s)',font=("Bold",18),padx=30).grid(row=1,column=1) 

emg_groups = {'Quadriceps' : 0, 
       'Hamstrings' : 0, 
       'Calves' : 0 
} 

for i, measure in enumerate(emg_groups): 
    emg_groups[measure] = IntVar() 
    emg_cb = Checkbutton(root, text=measure,  variable=emg_groups[measure],state=DISABLED) 
    emg_cb.grid(column=1, row=i+2, sticky='W', padx=30) 

emg1 = emg_groups['Quadriceps'].get() 
emg2 = emg_groups['Hamstrings'].get() 
emg3 = emg_groups['Calves'].get() 


ilabel3 = Label(root, text='Ground Reaction Force',font=("Bold",18),padx=30).grid(row=1,column=2) 

grf_groups = {'Ground Reaction Force' : 0, 
       'Gait' : 0, 
} 

for i, measure in enumerate(grf_groups): 
    grf_groups[measure] = IntVar() 
    grf_cb = Checkbutton(root, text=measure,  variable=grf_groups[measure],state=DISABLED) 
    grf_cb.grid(column=2, row=i+2, sticky='W', padx=30) 

grf1 = grf_groups['Ground Reaction Force'].get() 
grf2 = grf_groups['Gait'].get() 


#JOINT ANGLES - Both Acceleration and Angles 
ilabel4 = Label(root, text='Joints',font=("Bold",18)).grid(row=1,column=3) 

ja_groups = {'Hips' : 0, 
       'Knees' : 0, 
       'Ankles' : 0, 
} 

for i, measure in enumerate(ja_groups): 
    ja_groups[measure] = IntVar() 
    ja_cb = Checkbutton(root, text=measure, variable=ja_groups[measure],state=DISABLED) 
    ja_cb.grid(column=3, row=i+2, sticky='W', padx=20) 

ja1 = ja_groups['Hips'].get() 
ja2 = ja_groups['Knees'].get() 
ja3 = ja_groups['Ankles'].get() 

「主」 checkbutton組是數據類型字典/組,子組是EMG,GRF,和JA基。我真的不知道如何閱讀使用字典編寫的個人狀態/檢查按鈕的值,我也知道如何做到這一點也會對這個問題有所幫助。

謝謝你的時間。

+0

在你的代碼的縮進關閉。不可能知道哪些代碼屬於'enable_location_date',哪些不屬於'enable_location_date'。 –

+0

我的歉意,我糾正了縮進。由於鍵入的內容很多,我通常只是複製/粘貼Notepad ++中的內容並在其後添加縮進,但我似乎錯過了一些縮進。 – Abbas

回答

0

在這組代碼和其他類似的代碼中,您會覆蓋每個emg_cb,因此它指向僅創建的最後一個。這就是IntVar()的用途,所以請在enable_location_state()中使用emg_groups。用一個簡單的程序進行一點測試,可以看出什麼是有效的,什麼不可行。 Checkbutton http://effbot.org/tkinterbook/checkbutton.htm和Tkinter的變量引用http://effbot.org/tkinterbook/variable.htm

for i, measure in enumerate(emg_groups): 
    emg_groups[measure] = IntVar() 
    emg_cb = Checkbutton(root, text=measure, variable=emg_groups[measure],state=DISABLED) 
    emg_cb.grid(column=1, row=i+2, sticky='W', padx=30) 
+0

所以我很抱歉,如果它是明顯的,我已經錯過了,但我似乎無法得到它的工作。我接受了您的建議,並將emg_cb更改爲函數中的emg_groups,然後返回錯誤,指出您無法配置字典。如果我將其更改爲emg_group ['whatever'],則表示它說IntVar()沒有配置類型。即使我能夠使用intvar或emg_groups調用特定行,我如何從該數據配置checkbutton?我可以調用字典checkbutton的特定行嗎? – Abbas

+0

「,則表示它說IntVar()沒有配置類型」 - 這在上面的第二個鏈接中顯示。學習使用參考和教程。他們寫一次,閱讀很多原因。 –