簡體   English   中英

在 python 中訪問 StringVar() 作為普通字符串

[英]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 變量類BooleanVarDoubleVarIntVarStringVar

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM