【发布时间】:2021-02-21 13:30:49
【问题描述】:
我正在尝试制作一个仅在更新标签上显示分数的分数系统 - 分数信息 (score1) 设置在另一个函数中,我找不到解决方案。您可以忽略下面的代码,因为它只是我的绝望尝试和我缺乏理解
import tkinter
import random
score1 = 0
def example
global score1
n = random.randint(1,3)
if (n == 3)
score1 = score1 + 1
label = tkinter.Label(root, text=score1, font = ('', 30))
label.pack()
def update():
global score1
scoreIS = score1
label['text'] = scoreIS
root.after(100, update)
update()
root.mainloop()
edit: score1 =0 已添加但仍无法正常工作,标签显示但未更改
【问题讨论】:
-
把
label.config(text=score1)放在example函数的末尾 -
在
update函数内调用root.after(100, update)。另外,def example应该是def example():,我相信scoreIS = score1应该是scoreIS= example()。另外,请注意示例函数应返回score1