随机数和tkinter不工作

时间:2015-02-17 21:00:33

标签: python tkinter

我是python的新手,因为我只使用它4个月,我试图编写一个tkinter窗口,其中标签显示随机数,我到目前为止:

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 个答案:

答案 0 :(得分:0)

你似乎对我有用,这是一个稍微更简洁的版本,然而每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()
相关问题