试图使按钮文本更改。如果按钮具有LBS,则单击按钮应将其更改为KGS。如果按钮具有KGS,则单击按钮应将其更改为LBS。
该按钮什么也不做,但是代码对我来说似乎是正确的。
from Tkinter import *
myGui=Tk()
myGui.geometry("200x100")
myGui.title("basicGUI")
myGui.configure(bg="gray")
def toggle():
text = "LBS"
if text == LBS:
tglBtn.config(text = KGS)
else:
tglBtn.config(text = LBS)
LBS = StringVar
tglBtn = Button(text="LBS",
textvariable=LBS,
command=toggle)
tglBtn.pack()
mainloop()
StringVar
除非添加括号,否则您不会创建对象LBS = StringVar()
。该名称也具有误导性,因为该名称StringVar
将是“ LBS”或“ KGS”。a的全部意义StringVar
在于,只要StringVar
更改按钮的值,按钮的文本就会自动更新。
from Tkinter import *
myGui=Tk()
myGui.geometry("200x100")
myGui.title("basicGUI")
myGui.configure(bg="gray")
def toggle():
if buttonText.get() == "LBS":
buttonText.set("KGS")
else:
buttonText.set("LBS")
buttonText = StringVar()
buttonText.set("LBS") # you can't do StringVar("LBS")
tglBtn = Button(textvariable=buttonText,
command=toggle)
tglBtn.pack()
mainloop()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句