我的tkinter按钮只有一半时间有效

2024-06-26 17:46:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图学习编码,但我的按钮“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()

Tags: textimportyouhaverobotplacelabelfont
2条回答

在这里,您必须使用^{}

代码:

from tkinter import *
import tkinter.font as tkFont
import time

n=1
Robot=0
def BuyRobotCommand():
    global n
    global Robot
    Robot+=1

vindu=Tk()
vindu.title("Robo Wood")
geo="1920x1080"
vindu.geometry("1920x1080")


WoodCurrency=99
Money=0
WoodCutter=0


fontStyle=tkFont.Font(size=20)
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)


#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
labelRobot=Label(vindu, text="You have "+str(Robot)+" wood cutter robots", font=fontStyle)
labelRobot.place(x=500, y=300)
def main_loop():
    global delay, true, WoodCurrency, Money, WoodCutter

    #Fikser penger, og trær
    WoodCurrency+=1*Robot
    if WoodCurrency>0:
        Money+=1
        WoodCurrency-=1

    labelRobot.configure(text = "You have "+str(Robot)+" wood cutter robots")

    vindu.after(100, main_loop)
main_loop()


vindu.mainloop()

希望这有帮助

对标签textvariable使用StringVars怎么样

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(geo)

WoodCurrency = 99
WoodCurrencyLabel = StringVar(master=vindu,value="You have " + str(WoodCurrency) + " wood")
Money = 0
MoneyLabel = StringVar(master=vindu,value="You have " + str(Money) + " money")
WoodCutter = 0
RobotLabel = StringVar(master=vindu,value="You have " + str(Robot) + " wood cutter robots")

fontStyle=tkFont.Font(size=20)
labelWood=Label(vindu, textvariable=WoodCurrencyLabel, font=fontStyle).place(x=(1920/2)-100, y=100)
labelMoney=Label(vindu, textvariable=MoneyLabel, 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, textvariable=RobotLabel, font=fontStyle).place(x=500, y=300)
delay=0.1

while True:
    vindu.update()
    #Fikser penger, og trær
    WoodCurrency += 1 * Robot
    if WoodCurrency>0:
        Money += 1
        WoodCurrency -= 1
    WoodCurrencyLabel.set("You have " + str(WoodCurrency) + " wood")
    MoneyLabel.set("You have " + str(Money) + " money")
    RobotLabel.set("You have " + str(Robot) + " wood cutter robots")
    time.sleep(delay)

vindu.mainloop()

这样,您就可以更新变量,而无需每次重新创建标签小部件来显示它

相关问题 更多 >