如何在GUI打开后运行tkinter命令



tkinter gui打开后,我想每x秒更改一次名称,引用我的代码中的一个api

ws.mainloop()
# -----------------------------------------
starttime = time.time()
while True:
Value1 = data["session"]["gameType"]
Value2 = data["session"]["mode"]
Value3 = ' / '
Value =  Value1 + Value3 + Value2
ws.title(Value.lower())
time.sleep(60.0 - ((time.time() - starttime) % 60.0))

在gui打开下面的任何东西之后,直到程序关闭,它才会运行。

您可以使用.after()在给定延迟后运行函数:

after(ms, func=None, *args)
Call function once after given time.

'ms' specifies the time in milliseconds. 'func' gives the
function which shall be called. Additional parameters
are given as parameters to the function call.  Return
identifier to cancel scheduling with after_cancel.

下面是一个例子:

def update_title(starttime=time.time()):
Value1 = data["session"]["gameType"]
Value2 = data["session"]["mode"]
Value3 = ' / '
Value =  Value1 + Value3 + Value2
ws.title(Value.lower())
delay = 60.0 - ((time.time() - starttime) % 60.0)
# schedule next function execution
ws.after(int(delay*1000), update_title, starttime)
update_title() # start the periodic update task
ws.mainloop()

最新更新