最近在使用matplotlib绘图是总是提示:
Exception ignored in: <function Image.__del__ at 0x0000014E4A0E93A0> Traceback (most recent call last): File "C:\Users\admin\.conda\envs\python38\lib\tkinter\__init__.py", line 4017, in __del__ self.tk.call('image', 'delete', self.name) RuntimeError: main thread is not in main loop Exception ignored in: <function Image.__del__ at 0x0000014E4A0E93A0>
通过ChatGPT了解这个异常通常是由于Tkinter
在主线程以外的线程中执行了相关操作,从而导致了线程不匹配的问题。在Tkinter
中,很多操作必须在主事件循环(main loop)中执行,否则会出现异常。但是我并没有使用多线程绘图。进一步的chatGPT回复说即使您没有直接操作Tkinter,但是matplotlib底层仍然使用了Tkinter来管理图形窗口。并给出的解决方案为调用matplotlib.use('agg')
来设置matplotlib为后端模式,以避免Tkinter的使用。
import matplotlib
matplotlib.use('agg')
问题解决。