在python中将StringVar()作为普通字符串访问

2024-06-17 11:12:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我想访问selected 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:我知道这段代码中存在一些弊端,引入这些错误是为了使代码最小化和问题易于理解。在


Tags: oftextpyvaluevariableselectedanchorvoltage
1条回答
网友
1楼 · 发布于 2024-06-17 11:12:18

您可以像这样访问tkinter变量类BooleanVarDoubleVarIntVar、或{}的值:

my_variable = tk.StringVar()
my_variable.set('value')
print(my_variable.get())   # <  returns and prints the value contained in my_variable

有关详细信息,请参见here。在

相关问题 更多 >