python多线程的问题。运行代码后,在弹出的GUI界面里点击‘启动’,第一次点击时,能打印字符,当第二次点击‘启动’时,会报错。应该怎样修改bug呢?

Smallactive 2018-10-12 08:16:30
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()
...全文
307 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
凡s 2018-10-15
  • 打赏
  • 举报
回复
谢谢楼主分享

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧