python - 有没有办法在 Tkinter 中暂停和恢复线程?

标签 python python-3.x multithreading tkinter

我通常使用以下命令启动线程,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/

相关文章:

python - 写入不带任何小数位的 float

c# - 如果启动太多线程会怎样?

java - 重用 Runnable 对象

python - tensorflow 中的双线性上采样?

python - 如何将字符串解析为字典

python - 如何在 python 中创建字典列表?

python - Python3中读取二进制文件stdin时出现UnicodeDecodeError

python - 如何在函数签名中使用 *args 和 **kwargs 返回默认值

java - 使用 LinkedBlockingQueue 对多线程 java 程序足够好吗?

python - 如何在 Python 应用程序中使用 mercurial (Hg)?