Python 为什么我不能访问此matplotlib类的静态成员
我不明白为什么我不能从classPython 为什么我不能访问此matplotlib类的静态成员,python,matplotlib,Python,Matplotlib,我不明白为什么我不能从classNavigationToolbar2QT访问静态成员toolitems。为了调查这个问题,我做了以下工作: imp.find_module('matplotlib') >>> (None, '/usr/lib/pymodules/python2.7/matplotlib', ('', '', 5)) from matplotlib.backends.backend_qt4 import NavigationToolbar2QT print Na
NavigationToolbar2QT
访问静态成员toolitems
。为了调查这个问题,我做了以下工作:
imp.find_module('matplotlib')
>>> (None, '/usr/lib/pymodules/python2.7/matplotlib', ('', '', 5))
from matplotlib.backends.backend_qt4 import NavigationToolbar2QT
print NavigationToolbar2QT.toolitems
>>> AttributeError: type object 'NavigationToolbar2QT' has no attribute 'toolitems'
嗯。因此,现在我确定了模块matplotlib的位置,如果我打开usr/share/pyshared/matplotlib/backends/backends\u qt.py
,那么我会找到带有静态成员toolitems
的类NavigationToolbar2QT
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'),
)
现在,如果我执行以下操作:
imp.find_module('matplotlib')
>>> (None, '/usr/lib/pymodules/python2.7/matplotlib', ('', '', 5))
from matplotlib.backends.backend_qt4 import NavigationToolbar2QT
print NavigationToolbar2QT.toolitems
>>> AttributeError: type object 'NavigationToolbar2QT' has no attribute 'toolitems'
这就是我被卡住的地方,为什么我不能访问这个静态成员?我一定错过了一些琐碎的事情。问题解决了!正如布鲁诺所说,我走错了路。我正在查看usr/share/pyshared/matplotlib/backends/backends\u qt.py的代码,但是我正在从matplotlib.backends.backend\u qt4导入代码,这些是不同的模块。您正在查看usr/share/pyshared/matplotlib/backends/backends_qt.py的代码,但您正在从
matplotlib.backends.backend_qt4
导入代码-不是相同的模块。另外,如果我在github上浏览源代码,backend_qt4
从backend_qt5
()导入NavigationToolbar2QT
,它没有toolbar
类属性()Thx!我的头在键盘上弹得太厉害了,我错过了。这确实是问题所在,令人遗憾的是,我不能接受这是正确的答案,因为您已将其作为注释放置。来自matplotlib.backends.backend\u qt4
确实有。toolitems
,具体取决于您安装的matplotlib版本