gpt4 book ai didi

Python Tkinter While 线程

转载 作者:太空宇宙 更新时间:2023-11-03 16:07:00 25 4
gpt4 key购买 nike

嗯,我是 python 的新手,我很难在 Tkinter 中创建线程,正如你们都知道在 Tkinter 中使用 while 会导致它没有响应并且脚本仍在运行。

  def scheduler():
def wait():
schedule.run_pending()
time.sleep(1)
return
Hours = ScheduleTest()
if len(Hours) == 0:
print("You need to write Hours, Example: 13:30,20:07")
if len(Hours) > 0:
print("Scheduled: ", str(Hours))
if len(Hours) == 1:
schedule.every().day.at(Hours[0]).do(Jumper)
print("Will jump 1 time")
elif len(Hours) == 2:
schedule.every().day.at(Hours[0]).do(Jumper)
schedule.every().day.at(Hours[1]).do(Jumper)
print("Will jump 2 times")
elif len(Hours) == 3:
schedule.every().day.at(Hours[0]).do(Jumper)
schedule.every().day.at(Hours[1]).do(Jumper)
schedule.every().day.at(Hours[2]).do(Jumper)
print("Will jump 3 times")
while True:
t = threading.Thread(target=wait)
t.start()
return
scheduler()

我尝试过做类似的事情,但它仍然使 tkinter 没有响应提前致谢。

最佳答案

何时使用after方法;在没有线程的情况下进行伪造

正如评论中提到的,在大多数情况下,您不需要线程来运行“假”while 循环。您可以使用 after()方法来安排你的行动,使用 tkintermainloop作为一个“衣帽架”来安排事情,几乎就像在 while 循环中一样。

这适用于您可以简单地抛出命令的所有情况,例如subprocess.Popen() ,更新小部件,显示消息等。

当计划进程在主循环内运行需要大量时间时,它不起作用。因此time.sleep()真是太糟糕了;它只会保存 mainloop .

它是如何工作的

但是,在此限制内,您可以运行复杂的任务、计划操作甚至设置 break (-等效)条件。

只需创建一个函数,用 window.after(0, <function>) 启动它。在函数内部,(重新)使用 window.after(<time_in_milliseconds>, <function>) 安排函数.

要应用类似中断的条件,只需路由进程(在函数内部)即可不再调度。

一个例子

通过一个简化的示例可以最好地说明这一点:

enter image description here

enter image description here

enter image description here

from tkinter import *
import time

class TestWhile:

def __init__(self):

self.window = Tk()
shape = Canvas(width=200, height=0).grid(column=0, row=0)
self.showtext = Label(text="Wait and see...")
self.showtext.grid(column=0, row=1)
fakebutton = Button(
text="Useless button"
)
fakebutton.grid(column=0, row=2)

# initiate fake while
self.window.after(0, self.fakewhile)
self.cycles = 0

self.window.minsize(width=200, height=50)
self.window.title("Test 123(4)")
self.window.mainloop()

def fakewhile(self):
# You can schedule anything in here
if self.cycles == 5:
self.showtext.configure(text="Five seconds passed")
elif self.cycles == 10:
self.showtext.configure(text="Ten seconds passed...")
elif self.cycles == 15:
self.showtext.configure(text="I quit...")
"""
If the fake while loop should only run a limited number of times,
add a counter
"""
self.cycles = self.cycles+1
"""
Since we do not use while, break will not work, but simply
"routing" the loop to not being scheduled is equivalent to "break":
"""
if self.cycles <= 15:
self.window.after(1000, self.fakewhile)
else:
# start over again
self.cycles = 0
self.window.after(1000, self.fakewhile)
# or: fakebreak, in that case, uncomment below and comment out the
# two lines above
# pass

TestWhile()

在上面的示例中,我们运行计划进程十五秒。当循环运行时,函数 fakewhile() 会及时执行几个简单的任务。 .

在这十五秒之后,我们可以重新开始或“休息”。只需取消注释指定的部分即可查看...

关于Python Tkinter While 线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39689940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com