为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

狐的传说 2021-04-06 17:19:45
我想访问选定的单选按钮的值,并将其与if语句进行比较,但是在访问该值时,我得到的是PY_VAR0。from tkinter import *ComplaintForm=Tk()typesel=StringVar()#<--variable I'm using to access value of selected RadiobuttonHighVoltage=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 Radiobuttonsmainloop()PS:我知道此代码中引入了一些弊端,以使代码保持最小化,并使问题易于理解。
查看完整描述

1 回答

?
紫衣仙女

TA贡献1839条经验 获得超15个赞

您可以访问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


查看完整回答
反对 回复 2021-04-20
  • 1 回答
  • 0 关注
  • 1138 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号