首页 > 解决方案 > 我明白了,但铁标签不会更新

问题描述

我正在尝试制作一个简单的 GUI。铁、金、钻石和祖母绿变量在特定时间增加。我想让我的 while 循环在我的 tkinter 主循环运行时运行。任何帮助将不胜感激。谢谢!

我明白了,但我的铁标签不会更新。

import tkinter as tk

second=0
diamonds=0

#Team One Variables
i=0
g=0
d=0
e=0

teamOneHasEmeralds=False

#Team Two Variables
i2=0
g2=0
d2=0
e2=0

teamTwoHasEmeralds=False

#Setting Up Screen
root=tk.Tk()
root.title("Bedwars")
#screen = tk.Frame(root)
#screen.pack()

#Labels
iron = tk.Label(root, text=i)

def main():
    global i
    global i2
    global second
    i+=1
    i2+=1
    second+=1
    print("Team One Iron=",i)
    print("Team Two Iron=",i2)
    root.after(1000, main)
    iron.pack()
    root.mainloop()
    if second%3 == 0:
        global g
        global g2
        g+=1
        g2+=1
        print("Team One Gold=",g)
        print("Team Two Gold=",g2)
    if second%20==0:
        global diamonds
        if diamonds < 5:
            diamonds+=1
        print("Diamonds=",diamonds)
    if teamOneHasEmeralds and second%25==0:
        global e
        e+=1
        print("Team One Emeralds=",e)
    if teamTwoHasEmeralds and second%25==0:
        global e2
        e2+=1
        print("Team Two Emeralds=",e2)

root.after(1000, main)
root.mainloop()

标签: pythontkinter

解决方案


现在问题只是更新标签的文本值。为此,您不必iron.pack()重复调用,而只需调用一次(例如,在您的情况下,在您的 main 之前)。在您的函数中更新铁标签的文本属性iron["text"] = i

此外,您不需要root.mainloop()多次调用。


推荐阅读