鱼C论坛

 找回密码
 立即注册
查看: 1987|回复: 1

[已解决]mainloop()会接管主程序,请问如何让mainloop()之后的程序接着跑?

[复制链接]
发表于 2020-9-14 16:04:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

root = Tk()

listbox = Listbox(root)
listbox.pack(fill=BOTH, expand=True)

for i in range(10):
    listbox.insert(END, str(i))

mainloop()     #mainloop()会接管主程序,请问如何让mainloop()之后的程序接着跑?
print('窗口创建完成')  


最佳答案
2020-9-14 17:18:59
改造成多线程
  1. import tkinter as tk
  2. import threading

  3. def xiancheng1():
  4.     root = tk.Tk()
  5.     listbox = tk.Listbox(root)
  6.     listbox.pack(fill=tk.BOTH, expand=True)

  7.     for i in range(10):
  8.         listbox.insert(tk.END, str(i))

  9.     tk.mainloop()     #mainloop()会接管主程序,请问如何让mainloop()之后的程序接着跑?

  10. def xiancheng2():
  11.     print('窗口创建完成')

  12. if __name__ == '__main__':
  13.     t1=threading.Thread(target=xiancheng1)
  14.     t1.start()
  15.     t2=threading.Thread(target=xiancheng2)
  16.     t2.start()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-9-14 17:18:59 | 显示全部楼层    本楼为最佳答案   
改造成多线程
  1. import tkinter as tk
  2. import threading

  3. def xiancheng1():
  4.     root = tk.Tk()
  5.     listbox = tk.Listbox(root)
  6.     listbox.pack(fill=tk.BOTH, expand=True)

  7.     for i in range(10):
  8.         listbox.insert(tk.END, str(i))

  9.     tk.mainloop()     #mainloop()会接管主程序,请问如何让mainloop()之后的程序接着跑?

  10. def xiancheng2():
  11.     print('窗口创建完成')

  12. if __name__ == '__main__':
  13.     t1=threading.Thread(target=xiancheng1)
  14.     t1.start()
  15.     t2=threading.Thread(target=xiancheng2)
  16.     t2.start()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-27 06:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表