我正在尝试制作一个简单的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()
答案 0 :(得分:1)
现在问题只是在更新标签的文本值。为此,您不必重复调用iron.pack()
,而只需调用一次(例如,在您的情况下,在主电话之前)。在您的函数中,更新铁标签iron["text"] = i
您也无需多次致电root.mainloop()
。