我正在编写一个类似于cookie clicker的程序,以及" Hireprice"标签不会更新。这是我的代码。我不明白为什么标签不起作用,任何建议都会有所帮助。
from Tkinter import *
Cookies = 0
Helpers = 1
HirePrice = 100
CCK = Tk()
CCK.geometry("200x200")
CCK.title("Cookie Clicker")
CCookies = IntVar()
CCookies.set(Cookies)
def Bake():
global Cookies
global Helpers
Cookies = Cookies + Helpers
CCookies.set(Cookies)
def HelperHire():
global Helpers
global HirePrice
global CCookies
global Cookies
global HireLabel
global HireLabelTk
if Cookies >= HirePrice:
Helpers = Helpers + 1
Cookies = Cookies - HirePrice
CCookies.set(Cookies)
HirePrice = HirePrice * 2.5
HireLabel = "Hire Price: " + str(HirePrice)
HireLabelTk = StringVar()
HireLabelTk.set(HireLabel)
HireLabel = "Hire Price: " + str(HirePrice)
HireLabelTk = StringVar()
HireLabelTk.set(HireLabel)
Hireprice = Label(CCK,textvariable = HireLabelTk, padx = 20,pady = 10)
Hireprice.pack()
BakeButton = Button(CCK,text = "Bake!", padx = 15, pady = 1, width = 10, height = 1, command = Bake)
BakeButton.pack()
HireButton = Button(CCK,text = "Hire a helper!", padx = 15, pady = 1, width = 10, height = 1, command = HelperHire)
HireButton.pack()
CookieNum = Label(CCK,textvariable = CCookies, padx = 10,pady = 100)
CookieNum.pack()
CCK.mainloop()
答案 0 :(得分:0)
问题在于,每次更改值时,都会创建StringVar
的新实例,而不是更新与标签关联的实例。从HireLabelTk = StringVar()
移除HelperHire
。