[英]Access StringVar() as a normal string in python
我想访问选定 Radiobutton 的值并将其与 if 语句进行比较,但在访问该值时,我得到的是 PY_VAR0。
from tkinter import *
ComplaintForm=Tk()
typesel=StringVar()#<--variable I'm using to access value of selected Radiobutton
HighVoltage=Radiobutton(ComplaintForm,text='High Voltage Motor',value='HighVoltage',\
anchor=W,font='roboto 18',bg='white',variable=typesel)
HighVoltage.grid(row=5,column=1,padx=5,pady=10)
LowVoltage=Radiobutton(ComplaintForm,text='Low Voltage Motor',value='LowVoltage',\
anchor=W,font='roboto 18',bg='white',variable=typesel)
LowVoltage.grid(row=5,column=0,padx=5,pady=10)
print(typesel)#this is printing PY_VAR0 instead of accessing value of above Radiobuttons
mainloop()
PS:我知道这段代码中存在一些弊端,这些弊端是为了使代码最小化并使问题易于理解。
您可以像这样访问 tkinter 变量类BooleanVar
、 DoubleVar
、 IntVar
或StringVar
:
my_variable = tk.StringVar()
my_variable.set('value')
print(my_variable.get()) # <-- returns and prints the value contained in my_variable
请参阅此处了解更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.