“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
相关文章
|
6月前
|
Python
python RuntimeError: main thread is not in main loop
python RuntimeError: main thread is not in main loop
179 1
|
6月前
|
存储 前端开发 算法
C++线程 并发编程:std::thread、std::sync与std::packaged_task深度解析(一)
C++线程 并发编程:std::thread、std::sync与std::packaged_task深度解析
230 0
已解决 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'.异常错误,已解决
530 0
已解决 RuntimeError: There is no current event loop in thread ‘Thread-1‘.
|
2月前
|
数据库 Python
Traceback(most recent call last):File "main.py", line 4l,in<module>alueError: sleep length must be n
Traceback(most recent call last):File "main.py", line 4l,in<module>alueError: sleep length must be n
|
6月前
|
存储 并行计算 Java
C++线程 并发编程:std::thread、std::sync与std::packaged_task深度解析(二)
C++线程 并发编程:std::thread、std::sync与std::packaged_task深度解析
268 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()”
1214 0
|
5月前
|
Python
【ERROR】asyncio.run(main())报错:RuntimeError: Event loop is closed
【ERROR】asyncio.run(main())报错:RuntimeError: Event loop is closed
121 0
Exception in thread “main“ 主线程异常的解决方法
Exception in thread “main“ 主线程异常的解决方法
754 0
|
图形学
Unity 解决“... can only be called from the main thread”问题
“... can only be called from the main thread”
772 0
|
Java 调度 API
java之Thread.sleep(long)与object.wait()/object.wait(long)的区别及相关概念梳理(good)
一、Thread.sleep(long)与object.wait()/object.wait(long)的区别sleep(long)与wait()/wait(long)行为上有些类似,主要区别如下:1.Thread.sleep(long)是属于Thread类的静态方法。
1370 0