Tkinter Checkbutton状态更改使用词典

时间:2016-02-19 22:01:43

标签: python-2.7 if-statement dictionary tkinter

的StackOverflow,

所以我仍然在Tkinter工作这个GUI,并且我已经设置了我的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()

" main" checkbutton group是数据类型字典/组,子组是EMG,GRF和JA组。老实说,我也不知道如何阅读用词典制作的按钮的个别状态/值,我认为知道怎么做也会对这个问题有所帮助。

感谢您的时间。

1 个答案:

答案 0 :(得分: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)