2015-07-10 43 views
0

我不明白爲什麼我不能從類NavigationToolbar2QT訪問靜態成員toolitems。爲了調查這個問題,我做了以下幾點:爲什麼我不能訪問這個matplotlib類的靜態成員

imp.find_module('matplotlib') 
>>> (None, '/usr/lib/pymodules/python2.7/matplotlib', ('', '', 5)) 

好的。所以現在我敢肯定,當模塊matplotlib所在,如果我打開usr/share/pyshared/matplotlib/backends/backends_qt.py然後我找到類NavigationToolbar2QT靜態成員toolitems

class NavigationToolbar2QT(NavigationToolbar2, qt.QWidget): 
    # list of toolitems to add to the toolbar, format is: 
    # text, tooltip_text, image_file, callback(str) 
    toolitems = (
     ('Home', 'Reset original view', 'home.ppm', 'home'), 
     ('Back', 'Back to previous view','back.ppm', 'back'), 
     ('Forward', 'Forward to next view','forward.ppm', 'forward'), 
     (None, None, None, None), 
     ('Pan', 'Pan axes with left mouse, zoom with right', 'move.ppm', 'pan'), 
     ('Zoom', 'Zoom to rectangle','zoom_to_rect.ppm', 'zoom'), 
     (None, None, None, None), 
     ('Subplots', 'Configure subplots','subplots.png', 'configure_subplots'), 
     ('Save', 'Save the figure','filesave.ppm', 'save_figure'), 
     ) 

現在,如果我做了以下內容:

from matplotlib.backends.backend_qt4 import NavigationToolbar2QT 
print NavigationToolbar2QT.toolitems 
>>> AttributeError: type object 'NavigationToolbar2QT' has no attribute 'toolitems' 

這我有點卡住了,爲什麼我不能訪問這個靜態成員?我必須在這裏錯過一些微不足道的東西。

+1

它適用於我 –

+1

您正在查看usr/share/pyshared/matplotlib/backends/backends_qt.py的代碼,但是您正在從'matplotlib.backends.backend_qt4'導入代碼 - 不是相同的模塊。另外,如果我在github上瀏覽源代碼,'backend_qt4'從'backend_qt5'(https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/backends/backend_qt4.py#L35)導入'NavigationToolbar2QT' ,它沒有'工具欄'類屬性(https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/backends/backend_qt5.py#L566) –

+0

Thx!我正在用力撞擊鍵盤,以至於我錯過了這一點。這確實是問題所在,但我肯定不能接受這是正確的答案,因爲您已將此作爲評論。 – Gio

回答

0

問題解決了!正如布魯諾評論的那樣,我正在尋找錯誤的道路。我正在查看usr/share/pyshared/matplotlib/backends/backends_qt.py的代碼,但我正在導入代碼from matplotlib.backends.backend_qt4,這些模塊並不相同。

+0

'from matplotlib.backends.backend_qt4'確實具有'.toolitems',具體取決於您安裝的matplotlib版本 –

相關問題