python多线程的问题。运行代码后,在弹出的GUI界面里点击‘启动’,第一次点击时,能打印字符,当第二次点击‘启动’时,会报错。应该怎样修改bug呢?
python多线程的问题。运行代码后,在弹出的GUI界面里点击‘启动’,第一次点击时,能打印字符,当第二次点击‘启动’时,会报错。应该怎样修改bug呢?
import threading
import tkinter as tk
from time import ctime,sleep
# 创建主窗口
window = tk.Tk()
window.title('测试')
window.geometry('630x200')
def music():
for i in range(2):
print("I was listening to %s. %s")
sleep(1)
def move():
for i in range(2):
print("I was at the %s! %s")
sleep(1)
# 多线程
threads = []
t1 = threading.Thread(target=music)
threads.append(t1)
t2 = threading.Thread(target=move)
threads.append(t2)
def test():
for t in threads:
t.setDaemon(False)
t.start()
btn_download = tk.Button(window, text='启动', command=test)
btn_download.place(x=400, y=150)
window.mainloop()