繁体   English   中英

每秒刷新一次值 Tkinter

[英]Refresh value every second Tkinter

所以这里我有一个 function:

def test():
    global value

    if value ==0:
        value=1
        return True

    else:
        value=0
        return False

这是我想出的:

import tkinter as tk

value=0




while True:
    root = tk.Tk()
    root.title("my title")
    root.geometry('200x150')
    root.configure(background='black')

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

    def test():
        global value

        if value ==0:
            value=1
            return True

        else:
            value=0
            return False



    if test() is True:
        clock_frame.config(text="HelloWorld",fg="red")


    else:

        clock_frame.config(text="HelloWorld",fg="white")



    root.mainloop()

我想在 Tkinter GUI 中显示结果。 我想更改 label 而 function 是 True 或 False。 我希望这种变化每一秒都发生。 但我不知道该怎么做。

谢谢

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

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()

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

帮助这是结果像这样重复

账户总数:13 账户总数:13

def update():
        file = open("list.txt", "r")
        line_count = 0
        for line in file:
            if line != "\n":
              line_count += 1
        file.close()
        Label(kariata,bg="white", text=('Total Account:',(line_count)) , fg="green", font="bold").pack()

        kariata.after(1000, update) # call update() after 1 second
        
        
  update() # start the periodic update

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM