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

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

方法一(Tkinter)

最后写

root.mainloop()

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


方法二(多线程)

将线程设置为守护程序

t = threading.Thread(target=your_func)
t.setDaemon(True)
t.start()

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


方法三(matplotlib)

# do this before importing pylab or pyplot
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
相关文章
|
2月前
|
Python
python RuntimeError: main thread is not in main loop
python RuntimeError: main thread is not in main loop
119 1
|
11月前
已解决 RuntimeError: There is no current event loop in thread ‘Thread-1‘.
Jetson Xavier NX 报错 RuntimeError: There is no current event loop in thread 'Thread-1'.异常错误,已解决
191 0
已解决 RuntimeError: There is no current event loop in thread ‘Thread-1‘.
|
15天前
|
Python
【ERROR】asyncio.run(main())报错:RuntimeError: Event loop is closed
【ERROR】asyncio.run(main())报错:RuntimeError: Event loop is closed
13 0
|
开发工具 Android开发
解决bug:运行项目时报异常 “Can't create handler inside thread that has not called Looper.prepare()”
解决bug:运行项目时报异常 “Can't create handler inside thread that has not called Looper.prepare()”
958 0
|
10月前
Exception in thread “main“ 主线程异常的解决方法
Exception in thread “main“ 主线程异常的解决方法
568 0
|
10月前
|
Go 测试技术
init函数与main函数
init函数与main函数
init函数与main函数
|
10月前
|
图形学
Unity 解决“... can only be called from the main thread”问题
“... can only be called from the main thread”
539 0
|
12月前
End Sub 和 Exit Sub 的区别
End Sub 和 Exit Sub 的区别
97 0
End Sub 和 Exit Sub 的区别
|
12月前
|
C# C++
C#—Main方法
今天在敲委托代理第三个例子的时候遇到了这样的问题
_main函数 int __cdecl invoke_main(void) (?invoke_main@@YAHXZ)
_main函数 int __cdecl invoke_main(void) (?invoke_main@@YAHXZ)
_main函数 int __cdecl invoke_main(void) (?invoke_main@@YAHXZ)