![](/img/trans.png)
[英]%Matplotlib - AttributeError: 'NoneType' object has no attribute 'lower'
[英]Removing "help" tool from Matplotlib toolbar causes: AttributeError: 'NoneType' object has no attribute 'destroy'
在我的带有 Python 3.7.7 和 matplotlib 3.3.1 的 ubuntu 18.04 VM 中,此代码可以正常运行:
plt.rcParams['toolbar'] = 'toolmanager'
fig = plt.figure()
tm = fig.canvas.manager.toolmanager
tm.remove_tool('help') # Fails here in ubuntu in Azure!
但是当从我的 Azure DevOps 构建管道中的单元测试调用相同的代码时,它在ubuntu-18.04
Microsoft 托管的 VM 上的tm.remove_tool('help')
失败,并显示:
File "/opt/hostedtoolcache/Python/3.7.9/x64/lib/python3.7/site-packages/matplotlib/backend_managers.py", line 228, in remove_tool
tool.destroy()
AttributeError: 'NoneType' object has no attribute 'destroy'
这发生在ubuntu-18.04
Azure VM 上的 Python 3.6.12 和 3.7.9 中。 两者都在使用 matplotlib 3.3.1。
但是,相同的代码在 Python 3.6.8 和 3.7.9 以及 matplotlib 3.3.1 中的windows-2019
Microsoft 托管虚拟机上的 Windows 中运行时没有错误。
有没有其他人看到这个并得到修复或解决方法? 不幸的是,我无法在自己的 ubuntu VM 上重现此内容。
也许 Microsoft 托管的ubuntu-18.04
VM 缺少 matplotlib 需要的东西? 或者有一个奇怪的 matplotlib 错误? 我在matplotlib 3.1.1的时候在Azure没有看到这个问题。
2020 年 9 月 2 日更新
在初始化tm
后添加行print("Tools: %s" % tm._tools)
后,我发现tm._tools
是一个字典,在 Azure 中的 Windows 上有许多条目(而tm._tools['help']
是一个matplotlib.backends._backend_tk.HelpTk
对象)。 但是在 Linux on Azure tm._tools
中是一个空字典: {}
!
那么我需要为 Linux 中的 matplotlib 做一些额外的事情吗? 此处列出了 Azure 使用的 18.04 ubuntu VM 上的软件包,如果有帮助,请包括这些软件包:
2021 年 8 月 5 日更新
运行此修复了我自己的 ubuntu VM 中的问题:
$ sudo apt-get install python3-tk
我认为它安装了 Tcl/Tk 的后端库(参见此处)。 但不幸的是,此修复程序无法解决ubuntu-18.04
Azure VM 中的错误。
我不知道为什么 Azure 会悲伤地这样做。 不过,作为一种解决方法,您始终可以根据需要探索在没有工具栏的情况下绘制图形。
就像是:
import tkinter as tk
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
data = [i for i in range(5)]
window = tk.Tk()
artist = Figure()
artist.add_subplot().plot(data)
canvas = FigureCanvasTkAgg(artist, master=window)
canvas.draw()
canvas.get_tk_widget().pack()
window.mainloop()
事实证明,Tcl/Tk 无法在 ubuntu Azure DevOps VM 上运行,除非虚拟显示服务器在后台运行,例如 Xvfb。 您还需要将DISPLAY
环境变量设置为指向此显示。 有关详细信息,请在此处查看我的相关答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.