【问题标题】:Tkinter Checkbutton State Change using Dictionaries使用字典更改 Tkinter Checkbutton 状态
【发布时间】:2023-04-05 08:02:02
【问题描述】:

堆栈溢出,

所以我仍在 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()

“主”复选按钮组是数据类型字典/组,子组是 EMG、GRF 和 JA 组。老实说,我也不知道如何读取用字典制作的检查按钮的各个状态/值,而且我认为知道如何做到这一点也将有助于解决这个问题。

感谢您的宝贵时间。

【问题讨论】:

  • 代码中的缩进已关闭。无法分辨哪些代码属于enable_location_date,哪些不属于。
  • 抱歉,我已经更正了缩进。由于要键入的内容很多,我通常只是复制/粘贴我在 Notepad++ 中的内容,然后添加缩进,但我似乎遗漏了一些。

标签: python-2.7 if-statement dictionary tkinter


【解决方案1】:

在这组代码和其他类似代码中,您覆盖了每个 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)

【讨论】:

  • 所以我很抱歉,如果它很明显并且我错过了它,但我似乎无法让它发挥作用。我听取了您的建议,并将函数中的 emg_cb 更改为 emg_groups,然后返回错误,说明您无法配置字典。如果我将其更改为 emg_group['whatever'],那么它会说 IntVar() 没有配置类型。即使我能够使用 intvar 或 emg_groups 调用特定行,我将如何从该数据配置检查按钮?我可以调用字典检查按钮的特定行吗?
  • “然后它说 IntVar() 没有配置类型” - 如上面的第二个链接所示。学习使用参考资料和教程。它们写一次,读多次是有原因的。
猜你喜欢
  • 2015-05-16
  • 1970-01-01
  • 2020-10-21
  • 2020-05-04
  • 1970-01-01
  • 2022-01-02
  • 2012-02-20
  • 1970-01-01
  • 2020-12-05
相关资源
最近更新 更多