python - 我明白了,但铁标签不会更新
问题描述
我正在尝试制作一个简单的 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()
解决方案
现在问题只是更新标签的文本值。为此,您不必iron.pack()
重复调用,而只需调用一次(例如,在您的情况下,在您的 main 之前)。在您的函数中更新铁标签的文本属性iron["text"] = i
此外,您不需要root.mainloop()
多次调用。
推荐阅读
- python - 如何让 Jupyter notebook python 帮助函数输出丰富多彩?
- octave - “标量”操作未为“cs-list”实现二元运算符“*”
- java - 使用比较器按特定顺序对对象列表进行排序
- objectgears - ObjectGears 不会在报告过滤器中保存更改
- javascript - passrseInt() 的问题
- c# - 如何将 Request.Query/Form 对象作为 ASP.Net Core 5 中的一组“反序列化”参数传递给自定义方法?
- android - MockK - 是否支持在同一测试中使用不同的“exact =”多次调用 verify()?
- typescript - 如何在 TypeScript 中使用多态函数?
- c# - 有没有办法在 VS Code Winforms 中“格式化绘制”表单元素的格式?
- php - 币安 API。是否有可能获得一对在特定日期和时间交易的价值?