簡體   English   中英

從 Matplotlib 工具欄中刪除“幫助”工具會導致:AttributeError: 'NoneType' object has no attribute 'destroy'

[英]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 上的軟件包,如果有幫助,請包括這些軟件包:

  • libgtk-3-0
  • tk

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM