随机数和 tkinter 不工作

random number and tkinter not working

我是 python 的新手,因为我只用了 4 个月,我正在尝试编写一个 tkinter window,其中标签每秒显示一个随机数,我有这个到目前为止:

from tkinter import *
from random import *
testy = "0"
root = Tk()
lbl = Label(root,text="0")

def callback():
    global testy
    lbl.configure(text=testy)
    testy = str(randint(0,10))
    root.after(2000,callback)
lbl.pack()
root.after(2000,callback)
root.mainloop()

感谢任何帮助

你的似乎对我有用,这是一个稍微更精简的版本,但是每 1 秒而不是 2 次调用一次,因为这就是你在问题中概述的需求。

from tkinter import *
import random

root = Tk()
lbl = Label(root)
lbl.pack()

def replace_text():
    lbl.config(text=str(random.random()))
    root.after(1000, replace_text)

replace_text()
root.mainloop()