简体   繁体   English

从 Matplotlib 工具栏中删除“帮助”工具会导致:AttributeError: 'NoneType' object has no attribute 'destroy'

[英]Removing "help" tool from Matplotlib toolbar causes: AttributeError: 'NoneType' object has no attribute 'destroy'

In my ubuntu 18.04 VM with Python 3.7.7 and matplotlib 3.3.1 this code works without error:在我的带有 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!

But when the same code is called from a unittest in my Azure DevOps build pipeline, it fails at tm.remove_tool('help') on the ubuntu-18.04 Microsoft-hosted VM with:但是当从我的 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'

This happens in both Python 3.6.12 and 3.7.9 on the ubuntu-18.04 Azure VM.这发生在ubuntu-18.04 Azure VM 上的 Python 3.6.12 和 3.7.9 中。 Both are using matplotlib 3.3.1 as well.两者都在使用 matplotlib 3.3.1。

However the same code runs without error in Windows on a windows-2019 Microsoft-hosted VM in both Python 3.6.8 and 3.7.9, also both on matplotlib 3.3.1.但是,相同的代码在 Python 3.6.8 和 3.7.9 以及 matplotlib 3.3.1 中的windows-2019 Microsoft 托管虚拟机上的 Windows 中运行时没有错误。

Has anyone else seen this and got a fix or workaround please?有没有其他人看到这个并得到修复或解决方法? Unfortunately I can't reproduce this on my own ubuntu VM.不幸的是,我无法在自己的 ubuntu VM 上重现此内容。

Maybe the Microsoft-hosted ubuntu-18.04 VM is missing something matplotlib needs?也许 Microsoft 托管的ubuntu-18.04 VM 缺少 matplotlib 需要的东西? Or there's a weird matplotlib bug?或者有一个奇怪的 matplotlib 错误? I didn't see this issue in Azure when I was on matplotlib 3.1.1.我在matplotlib 3.1.1的时候在Azure没有看到这个问题。

Update on 2 Sept 2020 2020 年 9 月 2 日更新

After adding the line print("Tools: %s" % tm._tools) after initialising tm I find that tm._tools is a dict with many entries on Windows in Azure (and tm._tools['help'] is a matplotlib.backends._backend_tk.HelpTk object).在初始化tm后添加行print("Tools: %s" % tm._tools)后,我发现tm._tools是一个字典,在 Azure 中的 Windows 上有许多条目(而tm._tools['help']是一个matplotlib.backends._backend_tk.HelpTk对象)。 But in Linux on Azure tm._tools is an empty dict: {} !但是在 Linux on Azure tm._tools中是一个空字典: {}

So do I need to do something extra for matplotlib in Linux?那么我需要为 Linux 中的 matplotlib 做一些额外的事情吗? The packages on the 18.04 ubuntu VM used by Azure are listed here and include these, if it helps: 此处列出了 Azure 使用的 18.04 ubuntu VM 上的软件包,如果有帮助,请包括这些软件包:

  • libgtk-3-0 libgtk-3-0
  • tk tk

Update on 5 Aug 2021 2021 年 8 月 5 日更新

Running this fixes the issue in my own ubuntu VM:运行此修复了我自己的 ubuntu VM 中的问题:

$ sudo apt-get install python3-tk 

I think it installs the back-end libraries for Tcl/Tk (see here ).我认为它安装了 Tcl/Tk 的后端库(参见此处)。 But unfortunately this fix does not cure the error in the ubuntu-18.04 Azure VM.但不幸的是,此修复程序无法解决ubuntu-18.04 Azure VM 中的错误。

I don't have an answer as to why Azure is doing this sadly.我不知道为什么 Azure 会悲伤地这样做。 Though, as a workaround you could always explore plotting the figure without the toolbar, depending on your needs.不过,作为一种解决方法,您始终可以根据需要探索在没有工具栏的情况下绘制图形。

something like:就像是:

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()

It turns out Tcl/Tk won't work on an ubuntu Azure DevOps VM unless a virtual display server is running in the background, such as Xvfb.事实证明,Tcl/Tk 无法在 ubuntu Azure DevOps VM 上运行,除非虚拟显示服务器在后台运行,例如 Xvfb。 You also need to set the DISPLAY environment variable to point at this display.您还需要将DISPLAY环境变量设置为指向此显示。 See my related answer here for details.有关详细信息,请在此处查看我的相关答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 %Matplotlib - AttributeError: 'NoneType' 对象没有属性 'lower' - %Matplotlib - AttributeError: 'NoneType' object has no attribute 'lower' AttributeError:'NoneType'对象在__init__.py中没有属性'children' - AttributeError: 'NoneType' object has no attribute 'children' in __init__.py destroy 损坏的更新 Conda: AttributeError: 'NoneType' object has no attribute 'loader' for matplotlib - Broken Update Conda : AttributeError: 'NoneType' object has no attribute 'loader' for matplotlib 从YahooFinance获取价格数据的原因:AttributeError'nonetype'对象没有属性'text' - Getting price data from YahooFinance causes: AttributeError 'nonetype' object has no attribute 'text' AttributeError: 'NoneType' 对象没有属性 - AttributeError: 'NoneType' object has no attribute AttributeError:“ NoneType”对象没有属性“ a” - AttributeError: 'NoneType' object has no attribute 'a' AttributeError'NoneType'对象没有属性'upload_from_filename' - AttributeError 'NoneType' object has no attribute 'upload_from_filename' AttributeError:'NoneType'对象没有属性'split'? - AttributeError: 'NoneType' object has no attribute 'split'? Celery - AttributeError:'NoneType'对象没有属性'delay' - Celery - AttributeError: 'NoneType' object has no attribute 'delay' 'AttributeError:'NoneType'对象没有属性'to_dict' - 'AttributeError: 'NoneType' object has no attribute 'to_dict'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM