Python Matplotlib: 解决 Tcl_AsyncDelete: async handler deleted by the wrong thread

Python Matplotlib: 解决 Tcl_AsyncDelete: async handler deleted by the wrong thread

问题:
在同时使用PyQt5中的QThreadmatplotlib.pyplot时,虽然一开始所有的图形都能顺利生成,但是在关闭窗口的时候,就会生成以下的错误:

Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "C:\Users\XXX\Miniconda3\lib\site-packages\matplotlib\_pylab_helpers.py", line 73, in destroy_all
    manager.destroy()
  File "C:\Users\XXX\Miniconda3\lib\site-packages\matplotlib\backends\_backend_tk.py", line 561, in destroy
    self.window.destroy()
  File "C:\Users\XXX\Miniconda3\lib\tkinter\__init__.py", line 2061, in destroy
    for c in list(self.children.values()): c.destroy()
  File "C:\Users\XXX\Miniconda3\lib\tkinter\__init__.py", line 2305, in destroy
    self.tk.call('destroy', self._w)
RuntimeError: main thread is not in main loop
Exception ignored in: <bound method Image.__del__ of <tkinter.PhotoImage object at 0x0000022484EF57B8>>
Traceback (most recent call last):
  File "C:\Users\XXX\Miniconda3\lib\tkinter\__init__.py", line 3507, in __del__
    self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop
Exception ignored in: <bound method Image.__del__ of <tkinter.PhotoImage object at 0x000002248A604940>>
Traceback (most recent call last):
  File "C:\Users\XXX\Miniconda3\lib\tkinter\__init__.py", line 3507, in __del__
    self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop
Exception ignored in: <bound method Image.__del__ of <tkinter.PhotoImage object at 0x000002248A68D208>>
Traceback (most recent call last):
  File "C:\Users\XXX\Miniconda3\lib\tkinter\__init__.py", line 3507, in __del__
    self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop
Exception ignored in: <bound method Image.__del__ of <tkinter.PhotoImage object at 0x000002248A89C080>>
Traceback (most recent call last):
  File "C:\Users\XXX\Miniconda3\lib\tkinter\__init__.py", line 3507, in __del__
    self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop
Exception ignored in: <bound method Image.__del__ of <tkinter.PhotoImage object at 0x000002248A8EF828>>
Traceback (most recent call last):
  File "C:\Users\XXX\Miniconda3\lib\tkinter\__init__.py", line 3507, in __del__
    self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop
Exception ignored in: <bound method Image.__del__ of <tkinter.PhotoImage object at 0x000002248A90BA90>>
Traceback (most recent call last):
  File "C:\Users\XXX\Miniconda3\lib\tkinter\__init__.py", line 3507, in __del__
    self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop
Exception ignored in: <bound method Image.__del__ of <tkinter.PhotoImage object at 0x000002248A8EFF60>>
Traceback (most recent call last):
  File "C:\Users\XXX\Miniconda3\lib\tkinter\__init__.py", line 3507, in __del__
    self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop
Tcl_AsyncDelete: async handler deleted by the wrong thread

原因:
一开始我以为是多线程的问题,后来才发现是matplotlib.pyplot造成的问题。matplotlib.pyplot在运行的时候,是需要在主线程(Main Thread)上运行的,然而,我在使用多线程的时候,将使用matplotlib.pyplot的函数用在了子线程里面。之后也在matplotlib的官方FAQ找到了相应的原因,如下图:
Python Matplotlib: 解决 Tcl_AsyncDelete: async handler deleted by the wrong thread_第1张图片

解决方法:
将原本的import matplotlib.pyplot as plt修改成以下:

import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot as plt

参考链接:
StackOverflow: Tcl_AsyncDelete: async handler deleted by the wrong thread
Matplotlib的官方FAQ

你可能感兴趣的:(Python Matplotlib: 解决 Tcl_AsyncDelete: async handler deleted by the wrong thread)