admin管理员组

文章数量:1487745

“RuntimeError: main thread is not in main loop“的几种解决方案

方法一(Tkinter)

最后写

代码语言:javascript代码运行次数:0运行复制
root.mainloop()

当然,如果不是root,则应使用Tk对象的名称代替root。

方法二(多线程)

将线程设置为守护程序

代码语言:javascript代码运行次数:0运行复制
t = threading.Thread(target=your_func)
t.setDaemon(True)
t.start()

daemon默认是False,将其设置为True,再Start,就可以解决问题。daemon为True,就是我们平常理解的后台线程,用Ctrl-C关闭程序,所有后台线程都会被自动关闭。如果daemon属性是False,线程不会随主线程的结束而结束,这时如果线程访问主线程的资源,就会出错。

方法三(matplotlib)
代码语言:javascript代码运行次数:0运行复制
# do this before importing pylab or pyplot
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2024-10-12,如有侵权请联系 cloudcommunity@tencent 删除thread多线程后台解决方案线程

本文标签: “RuntimeError main thread is not in main loop“的几种解决方案