假设我们写这样的东西:
import threading
Button(root, command=threading.Thread(target=func1).start)
现在,如果我们单击该按钮一次就可以了,但我们尝试 再次点击按钮 然后出现错误 “线程只能执行一次” .那么,如何避免这种情况
最佳答案
编辑答案::
正如您在评论中澄清的那样,您可以重新定义 Button
每次点击允许它接受多次点击,从而创建多个 threads
按要求。
您可以在目标 func1()
中执行此操作或 callback
threading.Thread
的函数目的。
一个工作示例如下所示:
import tkinter as tk
import threading
def func1():
theButton.configure(command=threading.Thread(target=func1).start)
print('Do everything else here')
root = tk.Tk()
theButton = tk.Button(root, text='Start', command=threading.Thread(target=func1).start)
theButton.pack()
root.mainloop()
编辑:感谢 CoolCloud 提出了一种更好的方法来配置回调中的按钮 func1()
.
关于python - 如何在 tkinter 中运行多个线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63900749/