代码之家  ›  专栏  ›  技术社区  ›  snark

从Matplotlib工具栏中删除“帮助”工具会导致:AttributeError:“NoneType”对象没有属性“destroy”

  •  0
  • snark  · 技术社区  · 4 年前

    在我的ubuntu 18.04 VM中,使用Python 3.7.7和matplotlib 3.3.1,这段代码可以正常工作:

        plt.rcParams['toolbar'] = 'toolmanager'
        fig = plt.figure()
        tm = fig.canvas.manager.toolmanager
        tm.remove_tool('help') # Fails here in ubuntu in Azure!
    

    但是,当从我的Azure DevOps构建管道中的单元测试调用相同的代码时,它在 tm.remove_tool('help') ubuntu-18.04 Microsoft托管的VM具有:

    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'
    

    这在Python 3.6.12和3.7.9中都会发生 ubuntu-18.04 Azure虚拟机。两者都使用matplotlib 3.3.1。

    但是,相同的代码在Windows上运行时没有错误 windows-2019 微软在Python 3.6.8和3.7.9中托管了VM,也在matplotlib 3.3.1上托管了VM。

    有没有其他人看到了这一点,并得到了解决方案或变通方法?不幸的是,我无法在我自己的ubuntu VM上复制它。

    也许微软主持 ubuntu-18.04 VM缺少matplotlib需要的东西?或者有一个奇怪的matplotlib bug?当我使用matplotlib 3.1.1时,我在Azure中没有看到这个问题。

    2020年9月2日更新

    添加行后 print("Tools: %s" % tm._tools) 初始化后 tm 我发现 tm._tools 是Azure中Windows上有许多条目的字典(和 tm._tools['help'] 是a matplotlib.backends._backend_tk.HelpTk 对象)。但在Azure上的Linux中 tm._tools 是一个空字典: {} !

    那么,我需要在Linux中为matplotlib做额外的事情吗?Azure使用的18.04 ubuntu VM上的软件包如下 here 如果有帮助,请包括以下内容:

    • libgtk-3-0
    • tk

    2021年8月5日更新

    运行此程序可以修复我自己的ubuntu VM中的问题:

    $ sudo apt-get install python3-tk 
    

    我认为它安装了Tcl/Tk的后端库(参见 here ). 但不幸的是,此修复程序无法解决 ubuntu-18.04 Azure虚拟机。

    0 回复  |  直到 3 年前
        1
  •  0
  •   Gibus    4 年前

    我不知道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()
    
        2
  •  0
  •   snark    2 年前

    事实证明,Tcl/Tk在ubuntu Azure DevOps VM上无法工作,除非虚拟显示服务器(如Xvfb)在后台运行。您还需要设置 DISPLAY 指向此显示器的环境变量。查看我的相关答案 here 了解详情。