简体   繁体   English

Tkinter ttk单选按钮变量更新失败

[英]Tkinter ttk radiobutton variable update failure

What's wrong with this? 这怎么了

var = StringVar()
radBut1 = ttk.Radiobutton(root, text='A - Z', variable=var, value='AtoZ')
radBut2= ttk.Radiobutton(root, text='Z - A', variable=var, value='ZtoA')

When I select either of the 2 radiobuttons, the var variable is not updated. 当我选择两个单选按钮之一时,var变量不会更新。 I found a lot of conflicting material in different documentation sources and tutorials (and I tried everything I found) to no avail. 我在不同的文档来源和教程中发现了很多相互矛盾的内容(并且尝试了所有发现的内容),但都无济于事。

I'm using the following to check the value of var: 我正在使用以下方法来检查var的值:

print(var)

This works: 这有效:

def updateRadioButtonVariable(radioButtonVar,updateString):
    radioButtonVar.set(updateString)

#Sort Type Radio Button
radioButtonVar = StringVar()
radBut1= ttk.Radiobutton(root, text='A - Z', variable=radioButtonVar, value='AtoZ',command= lambda: updateRadioButtonVariable(radioButtonVar ,'AtoZ'))
radBut2= ttk.Radiobutton(root, text='Z - A', variable=radioButtonVar, value='ZtoA',command= lambda: updateRadioButtonVariable(radioButtonVar ,'ZtoA'))

Not sure why the original code (which is almost template code from the documentation) doesn't work. 不知道为什么原始代码(几乎是文档中的模板代码)不起作用。 If anyone has any ideas, please do share. 如果有人有任何想法,请分享。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM