--update: 我改变Tkinter的单选按钮不更新变量
variable=self.optionVal.get()
到
variable=self.optionVal
但没有changed.Also我不知道为什么它会自动调用编译时self.selected?
----原文:
我试图让熟悉单选按钮,但我不认为我了解单选按钮的作品。下面是演示的简要代码:
self.optionVal = StringVar()
for text, val in OPTIONS:
print(text,val)
radioButton = Radiobutton(self,
text=text,
value=val,
variable=self.optionVal.get(),
command = self.selected())
radioButton.pack(anchor=W)
def selected(self):
print("this option is :"+self.optionVal.get())
In my opinion this should work like once I choose certain button, and it prints out "this option is *the value*", however now what it does is once compiled, it prints out everything, and the self.optionVal.get() is blankspace, as if value wasn't set to that variable.
I wonder what happens to my code,
Many thanks in advance.
您需要将**实际变量**作为变量参数传递,而不是调用变量的''.get()''的结果。 – jasonharper
@jasonharper我试过了,但还是没有通过 – angieShroom