我通常使用以下命令启动线程,threading.Thread(target=function_name).start()
有什么办法可以暂停它threading.Thread(target=name).wait()
简历就像threading.Thread(target=name).set()
最佳答案
检查如何this项目。该项目满足您的所有要求。 Python 的内置线程模块目前不支持播放和暂停功能。
所以要安装这些模块:pip install pythreadworker
安装后这里是一个例子:
from worker import Worker # We import the Worker to create thread
def some_long_running_task(foo, bar, tee="foo"):
do_random_long_stuff()
# ...
if __name__ == '__main__':
# to make a thread we initialise Worker or somewhat so called the Thread of threading module
thread = Worker(some_long_running_task) # remember just pass the function without execution parenthesis
# Now to start thread while also supplying arguments, we call start method
thread.start("bar", bar="foo", tee=123)
# To pause a the thread we use pause method
thread.pause()
# To resume it we use resume method
thread.resume()
# To use the Thread.join Worker class, use the join method
thread.join()
# And to kill it we use
thread.stop()
但是,如果您使用 time.sleep
,请记住使用它。那么你将不得不用
worker.sleep
替换它所以例如你导入代码是
from time import sleep
将其替换为from worker import sleep
希望这可以帮助。
关于python - 有没有办法在 Tkinter 中暂停和恢复线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65459138/