我试图学习编码,但我的按钮“BuyRobot”只在某些时候起作用,我不知道为什么。我希望能够多次按下按钮,对于显示我需要更新多少的标签(labelRobot),我认为这与处于循环中有关,但我尝试将其置于循环之外,但似乎无法使其工作。任何帮助都将不胜感激
from tkinter import *
import tkinter.font as tkFont
import time
n=1
Robot=0
def BuyRobotCommand():
global n
global Robot
Robot+=n
vindu=Tk()
vindu.title("Robo Wood")
geo="1920x1080"
vindu.geometry("1920x1080")
WoodCurrency=99
Money=0
WoodCutter=0
fontStyle=tkFont.Font(size=20)
#labelWood=Label(vindu, text="You have "+str(WoodCurrency)+" wood", font=fontStyle).place(x=(1920/2)-100, y=100)
#BuyRobot=Button(vindu, text="Kjøp en robot", font=fontStyle, command=BuyRobotCommand(1)).place(x=300, y=300)
true=1
delay=0.1
while true:
vindu.update()
#Fikser penger, og trær
WoodCurrency+=1*Robot
labelWood=Label(vindu, text="You have "+str(WoodCurrency)+" wood", font=fontStyle).place(x=(1920/2)-100, y=80)
if WoodCurrency>0:
Money+=1
WoodCurrency-=1
labelMoney=Label(vindu, text="You have "+str(Money)+" money", font=fontStyle).place(x=(1920/2)-100, y=130)
BuyRobot=Button(vindu, text="Kjøp en robot", font=fontStyle, command=BuyRobotCommand).place(x=300, y=300)
labelRobot=Label(vindu, text="You have "+str(Robot)+" wood cutter robots", font=fontStyle).place(x=500, y=300)
time.sleep(delay)
vindu.mainloop()
在这里,您必须使用^{}
代码:
希望这有帮助
对标签textvariable使用StringVars怎么样
这样,您就可以更新变量,而无需每次重新创建标签小部件来显示它
相关问题 更多 >
编程相关推荐