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找到了相应的原因,如下图:
在这里插入图片描述

解决方法:
将原本的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

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值