[英]Refresh value every second Tkinter
So Here i have a function:所以这里我有一个 function:
def test():
global value
if value ==0:
value=1
return True
else:
value=0
return False
Here is what I come up with:这是我想出的:
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()
I want to display the result of this in a Tkinter GUI.我想在 Tkinter GUI 中显示结果。 I want to change a label while the function is True or False.
我想更改 label 而 function 是 True 或 False。 I want this change to happen every second.
我希望这种变化每一秒都发生。 But i don't know how to do it.
但我不知道该怎么做。
Thank you谢谢
You can use after()
to periodically call test()
and update the label:您可以使用
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()
Use the after method then.然后使用 after 方法。 The syntax is like this: widget.after(milisecond, action).
语法是这样的:widget.after(milisecond, action)。 First you add the time to wait and next you add the action to do.
首先添加等待时间,然后添加要执行的操作。
help this is result recurs like this帮助这是结果像这样重复
Total Account: 13 Total Account: 13账户总数: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.