使用 Tkinter 在特定时间打印值 [英] Print values at specific time with Tkinter
问题描述
我编写了一个基于命令的程序,它运行正常.由于我想添加一些图形界面,所以我用 Tkinter 准备了一个简单的界面.但是包含scheduler.run"的那一行,它锁定了我的应用程序,我无法得到任何错误代码.
I've written a command-base program and it works properly. Since I'd like to add some graphical interface, I prepared a simple one with Tkinter. But the line which contains "scheduler.run", it locks my application and I can't get any error code.
def do_deneme(p):
etiket_run1["text"] = etiket_run1["text"] + str(p) + " completed at " + str(datetime.datetime.now())
def run_do():
...
scheduler=sched.scheduler(time.time, time.sleep)
for p in clean_information:
scheduler.enter(float(p[12]), 1, do_deneme,(p,))
etiket_run1["text"] = etiket_run1["text"] + str(datetime.datetime.now())
scheduler.run()
etiket_run1["text"] = etiket_run1["text"] + "Completed."
...
etiket_run1=Label(cerceve1, fg="red")
etiket_run1.pack(side=BOTTOM,padx=5,pady=5)
dugme = Button(cerceve2,text=u"Start",command=run_do)
...
有没有办法调试这个代码部分?或者任何关于在 Tkinter 中使用带有标签的 scheduler.run 的建议?
Any way to debug this code part? or any suggestion about using scheduler.run with labels in Tkinter?
推荐答案
Tkinter 是单线程的.看起来您的调度程序在运行某些东西之前一直处于休眠状态,因此当它处于休眠状态时,您的 GUI 将锁定.
Tkinter is single-threaded. It appears that your scheduler sleeps until it is time to run something, so while it is sleeping your GUI will lock up.
使用 Tkinter 在未来或按计划运行某事的正确方法是调用 after
,它使用事件循环来安排某事在设定的时间后运行.如果您希望某事在固定的毫秒数后运行,您可以调用它一次,或者您可以重复调用它直到某个条件.重复,我的意思是你使用 after
来调用一个检查条件的函数;如果条件为假,函数将使用 after
再次调用自身.如果条件为真,它将运行您的作业.
The proper way to run something in the future or on a schedule with Tkinter is to call after
which uses the event loop to schedule something to run after a set amount of time. You can either call it once if you want something to run a fixed number of milliseconds later, or you can repeatedly call it until some condition. By repeated, I mean you use after
to call a function which checks the condition; if the condition is false the function will call itself again with after
. If the condition is true, it runs your job.
这篇关于使用 Tkinter 在特定时间打印值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!