每秒刷新一次Tkinter

时间:2020-08-09 13:00:40

标签: python user-interface tkinter

所以这里我有一个功能:

$("#a").click(function() {
  $(this).animate({
      margin-left: "50%"
    }, "slow")
    .animate({
      margin-top: "-100px"
    }, "slow");
});

这是我想出的:

def test():
    global value

    if value ==0:
        value=1
        return True

    else:
        value=0
        return False

我想在Tkinter GUI中显示此结果。我想在功能为True或False时更改标签。我希望这种变化每秒钟发生一次。 但是我不知道该怎么做。

谢谢

2 个答案:

答案 0 :(得分:0)

然后使用after方法。语法是这样的:widget.after(毫秒,动作)。首先,您要添加等待时间,然后添加要执行的操作。

答案 1 :(得分:0)

您可以使用after()定期调用test()并更新标签:

import tkinter as tk

root = tk.Tk()
root.geometry('+100+100')
root.config(bg='black')

clock = tk.Label(root, text='HelloWorld', font=('caviar dreams', 130), bg='black')
clock.pack()

value = 0

def test():
    global value
    value = 1 - value
    return value == 1

def update():
    color = 'red' if test() else 'white'
    clock.config(fg=color)
    root.after(1000, update) # call update() after 1 second

update() # start the periodic update
root.mainloop()