matplotlib navtoolbar 在 wx 2.9 (Mac OS X) 中无法实现

发布于 2024-11-09 12:08:44 字数 4273 浏览 0 评论 0原文

以下代码在 wxPython 2.8.x、Python 2.5.4/2.6、Matplotlib 0.99.x 上运行时,按预期在框架顶部生成 matplotlib NavToolbar2。

不过,我最近转向了 Python 2.7 和 wxPython 2.9.1,试图支持 64 位的 OS X。正是在这种环境下,下面的代码会生成一个空工具栏:

empty Toolbar

我注意到在构建 matplotlib 时它说了一些关于 not wx 2.9 及更高版本需要 WxAgg,这可能是问题所在吗?到目前为止,我所尝试的只是将 FigureCanvasWxAgg 替换为 FigureCanvasWx,将 NavigationToolbar2WxAgg 替换为 NavigationToolbar2Wx。运气不好。

有什么想法吗?

import wx
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg, NavigationToolbar2WxAgg
import matplotlib as mpl

app = wx.PySimpleApp()
f = wx.Frame(None)
fig = mpl.figure.Figure()
p = FigureCanvasWxAgg(f, -1, fig)
tb = NavigationToolbar2WxAgg(fig.canvas)
f.SetToolBar(tb)
tb.Realize()
f.Show()
app.MainLoop()

还有一件事......如果我用我自己的自定义导航工具栏类替换 NavigationToolbar2WxAgg (此线程上第一个答案的代码: 在 matplotlib 中添加新的导航模式),除非我删除 tb.Realize(),否则整个系统都会崩溃。

2011-05-25 08:21:18.354 Python[48013:60f] *** Assertion failure in -[NSToolbar _itemAtIndex:], /SourceCache/AppKit/AppKit-1038.35/Toolbar.subproj/NSToolbar.m:1227
2011-05-25 08:21:18.356 Python[48013:60f] An uncaught exception was raised
2011-05-25 08:21:18.356 Python[48013:60f] Invalid parameter not satisfying: index>=0 && index<[self _numberOfItems]
2011-05-25 08:21:18.358 Python[48013:60f] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: index>=0 && index<[self _numberOfItems]'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x00007fff868ef7b4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x00007fff83e9b0f3 objc_exception_throw + 45
    2   CoreFoundation                      0x00007fff868ef5d7 +[NSException raise:format:arguments:] + 103
    3   Foundation                          0x00007fff86d7c77e -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198
    4   AppKit                              0x00007fff806f44a9 -[NSToolbar _itemAtIndex:] + 158
    5   AppKit                              0x00007fff806f3e94 -[NSToolbar _removeItemAtIndex:notifyDelegate:notifyView:notifyFamilyAndUpdateDefaults:] + 56
    6   libwx_osx_cocoau-2.9.1.0.0.dylib    0x0000000101aea2a6 _ZN9wxToolBar7RealizeEv + 1430
    7   _controls_.so                       0x00000001048d4b76 _wrap_ToolBarBase_Realize + 102
    8   .Python                             0x00000001000ca81a PyEval_EvalFrameEx + 23498
    9   .Python                             0x00000001000cc8c5 PyEval_EvalCodeEx + 1733
    10  .Python                             0x00000001000ca9bf PyEval_EvalFrameEx + 23919
    11  .Python                             0x00000001000cc8c5 PyEval_EvalCodeEx + 1733
    12  .Python                             0x00000001000cb0c8 PyEval_EvalFrameEx + 25720
    13  .Python                             0x00000001000cc8c5 PyEval_EvalCodeEx + 1733
    14  .Python                             0x00000001000ca9bf PyEval_EvalFrameEx + 23919
    15  .Python                             0x00000001000cb2a6 PyEval_EvalFrameEx + 26198
    16  .Python                             0x00000001000cb2a6 PyEval_EvalFrameEx + 26198
    17  .Python                             0x00000001000cc8c5 PyEval_EvalCodeEx + 1733
    18  .Python                             0x00000001000ca9bf PyEval_EvalFrameEx + 23919
    19  .Python                             0x00000001000cb2a6 PyEval_EvalFrameEx + 26198
    20  .Python                             0x00000001000cc8c5 PyEval_EvalCodeEx + 1733
    21  .Python                             0x00000001000ccbc6 PyEval_EvalCode + 54
    22  .Python                             0x00000001000f0c7e PyRun_FileExFlags + 174
    23  .Python                             0x00000001000f1aa1 PyRun_SimpleFileExFlags + 817
    24  .Python                             0x00000001001093d9 Py_Main + 2825
    25  Python                              0x0000000100000f54 0x0 + 4294971220
)
terminate called after throwing an instance of 'NSException'

The following piece of code produces a matplotlib NavToolbar2 at the top of a frame as expected when run on wxPython 2.8.x, Python 2.5.4/2.6, Matplotlib 0.99.x.

However, I've recently moved to Python 2.7, and wxPython 2.9.1 in an attempt to support OS X in 64-bit. It's under this environment that the code below produces an empty toolbar:

empty toolbar

I noticed when building matplotlib that it said something about not needing WxAgg for wx 2.9 and up, might this be the problem? All I've tried so far is to replace FigureCanvasWxAgg with FigureCanvasWx and NavigationToolbar2WxAgg with NavigationToolbar2Wx. No luck.

Any ideas what's going on?

import wx
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg, NavigationToolbar2WxAgg
import matplotlib as mpl

app = wx.PySimpleApp()
f = wx.Frame(None)
fig = mpl.figure.Figure()
p = FigureCanvasWxAgg(f, -1, fig)
tb = NavigationToolbar2WxAgg(fig.canvas)
f.SetToolBar(tb)
tb.Realize()
f.Show()
app.MainLoop()

One more thing... if I replace the NavigationToolbar2WxAgg with my own custom navtoolbar class (code at first answer on this thread: Add new navigate modes in matplotlib), the whole thing crashes unless I remove tb.Realize().

2011-05-25 08:21:18.354 Python[48013:60f] *** Assertion failure in -[NSToolbar _itemAtIndex:], /SourceCache/AppKit/AppKit-1038.35/Toolbar.subproj/NSToolbar.m:1227
2011-05-25 08:21:18.356 Python[48013:60f] An uncaught exception was raised
2011-05-25 08:21:18.356 Python[48013:60f] Invalid parameter not satisfying: index>=0 && index<[self _numberOfItems]
2011-05-25 08:21:18.358 Python[48013:60f] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: index>=0 && index<[self _numberOfItems]'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x00007fff868ef7b4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x00007fff83e9b0f3 objc_exception_throw + 45
    2   CoreFoundation                      0x00007fff868ef5d7 +[NSException raise:format:arguments:] + 103
    3   Foundation                          0x00007fff86d7c77e -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198
    4   AppKit                              0x00007fff806f44a9 -[NSToolbar _itemAtIndex:] + 158
    5   AppKit                              0x00007fff806f3e94 -[NSToolbar _removeItemAtIndex:notifyDelegate:notifyView:notifyFamilyAndUpdateDefaults:] + 56
    6   libwx_osx_cocoau-2.9.1.0.0.dylib    0x0000000101aea2a6 _ZN9wxToolBar7RealizeEv + 1430
    7   _controls_.so                       0x00000001048d4b76 _wrap_ToolBarBase_Realize + 102
    8   .Python                             0x00000001000ca81a PyEval_EvalFrameEx + 23498
    9   .Python                             0x00000001000cc8c5 PyEval_EvalCodeEx + 1733
    10  .Python                             0x00000001000ca9bf PyEval_EvalFrameEx + 23919
    11  .Python                             0x00000001000cc8c5 PyEval_EvalCodeEx + 1733
    12  .Python                             0x00000001000cb0c8 PyEval_EvalFrameEx + 25720
    13  .Python                             0x00000001000cc8c5 PyEval_EvalCodeEx + 1733
    14  .Python                             0x00000001000ca9bf PyEval_EvalFrameEx + 23919
    15  .Python                             0x00000001000cb2a6 PyEval_EvalFrameEx + 26198
    16  .Python                             0x00000001000cb2a6 PyEval_EvalFrameEx + 26198
    17  .Python                             0x00000001000cc8c5 PyEval_EvalCodeEx + 1733
    18  .Python                             0x00000001000ca9bf PyEval_EvalFrameEx + 23919
    19  .Python                             0x00000001000cb2a6 PyEval_EvalFrameEx + 26198
    20  .Python                             0x00000001000cc8c5 PyEval_EvalCodeEx + 1733
    21  .Python                             0x00000001000ccbc6 PyEval_EvalCode + 54
    22  .Python                             0x00000001000f0c7e PyRun_FileExFlags + 174
    23  .Python                             0x00000001000f1aa1 PyRun_SimpleFileExFlags + 817
    24  .Python                             0x00000001001093d9 Py_Main + 2825
    25  Python                              0x0000000100000f54 0x0 + 4294971220
)
terminate called after throwing an instance of 'NSException'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悸初 2024-11-16 12:08:44

这看起来像是一个错误,但我不知道问题是否出在 matplotlib、wxPython 或 backend_wx.py 中。我修复它的方法是查看 backend_wx.py 源代码 工具栏并将其直接放在我的 wxPython 工具栏中。所以这对我有用:

import os
import wx
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg, NavigationToolbar2WxAgg
from matplotlib.backends.backend_wx import _load_bitmap
import matplotlib as mpl


app = wx.PySimpleApp()
f = wx.Frame(None)
fig = mpl.figure.Figure()
p = FigureCanvasWxAgg(f, -1, fig)

toolbar = NavigationToolbar2WxAgg(p)
toolbar.Hide()

#toolbar constants
TBFLAGS = (wx.TB_HORIZONTAL|wx.TB_TEXT)      
tsize = (24,24)
tb = f.CreateToolBar(TBFLAGS)

_NTB2_HOME = wx.NewId()
_NTB2_BACK = wx.NewId()
_NTB2_FORWARD = wx.NewId()
_NTB2_PAN = wx.NewId()
_NTB2_ZOOM = wx.NewId()
_NTB2_SAVE = wx.NewId()
_NTB2_SUBPLOT = wx.NewId()

tb.AddSimpleTool(_NTB2_HOME, _load_bitmap('home.png'), 'Home', 'Reset original view')
tb.AddSimpleTool(_NTB2_BACK, _load_bitmap('back.png'), 'Back', 'Back navigation view')
tb.AddSimpleTool(_NTB2_FORWARD, _load_bitmap('forward.png'), 'Forward', 'Forward navigation view')

tb.AddCheckTool(_NTB2_PAN, _load_bitmap('move.png'), shortHelp='Pan', longHelp='Pan with left, zoom with right')
tb.AddCheckTool(_NTB2_ZOOM, _load_bitmap('zoom_to_rect.png'), shortHelp='Zoom', longHelp='Zoom to rectangle')

tb.AddSeparator()
tb.AddSimpleTool(_NTB2_SUBPLOT, _load_bitmap('subplots.png'), 'Configure subplots', 'Configure subplot parameters')
tb.AddSimpleTool(_NTB2_SAVE, _load_bitmap('filesave.png'), 'Save', 'Save plot contents to file')

f.Bind(wx.EVT_TOOL, toolbar.home, id=_NTB2_HOME)
f.Bind(wx.EVT_TOOL, toolbar.forward, id=_NTB2_FORWARD)
f.Bind(wx.EVT_TOOL, toolbar.back, id=_NTB2_BACK)
f.Bind(wx.EVT_TOOL, toolbar.zoom, id=_NTB2_ZOOM)
f.Bind(wx.EVT_TOOL, toolbar.pan, id=_NTB2_PAN)
f.Bind(wx.EVT_TOOL, toolbar.configure_subplot, id=_NTB2_SUBPLOT)
f.Bind(wx.EVT_TOOL, toolbar.save, id=_NTB2_SAVE)

tb.Realize()  

tb.Realize()
f.Show()
app.MainLoop()

这是一个快速修复,但我猜有人必须为此提交一份错误报告。

It seems like a bug, but I do not know if the problem lies in matplotlib, in wxPython or in the backend_wx.py. What I did to fix it is that I looked in the backend_wx.py source code for the Toolbar and put it directly in my wxPython toolbar. So this works for me:

import os
import wx
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg, NavigationToolbar2WxAgg
from matplotlib.backends.backend_wx import _load_bitmap
import matplotlib as mpl


app = wx.PySimpleApp()
f = wx.Frame(None)
fig = mpl.figure.Figure()
p = FigureCanvasWxAgg(f, -1, fig)

toolbar = NavigationToolbar2WxAgg(p)
toolbar.Hide()

#toolbar constants
TBFLAGS = (wx.TB_HORIZONTAL|wx.TB_TEXT)      
tsize = (24,24)
tb = f.CreateToolBar(TBFLAGS)

_NTB2_HOME = wx.NewId()
_NTB2_BACK = wx.NewId()
_NTB2_FORWARD = wx.NewId()
_NTB2_PAN = wx.NewId()
_NTB2_ZOOM = wx.NewId()
_NTB2_SAVE = wx.NewId()
_NTB2_SUBPLOT = wx.NewId()

tb.AddSimpleTool(_NTB2_HOME, _load_bitmap('home.png'), 'Home', 'Reset original view')
tb.AddSimpleTool(_NTB2_BACK, _load_bitmap('back.png'), 'Back', 'Back navigation view')
tb.AddSimpleTool(_NTB2_FORWARD, _load_bitmap('forward.png'), 'Forward', 'Forward navigation view')

tb.AddCheckTool(_NTB2_PAN, _load_bitmap('move.png'), shortHelp='Pan', longHelp='Pan with left, zoom with right')
tb.AddCheckTool(_NTB2_ZOOM, _load_bitmap('zoom_to_rect.png'), shortHelp='Zoom', longHelp='Zoom to rectangle')

tb.AddSeparator()
tb.AddSimpleTool(_NTB2_SUBPLOT, _load_bitmap('subplots.png'), 'Configure subplots', 'Configure subplot parameters')
tb.AddSimpleTool(_NTB2_SAVE, _load_bitmap('filesave.png'), 'Save', 'Save plot contents to file')

f.Bind(wx.EVT_TOOL, toolbar.home, id=_NTB2_HOME)
f.Bind(wx.EVT_TOOL, toolbar.forward, id=_NTB2_FORWARD)
f.Bind(wx.EVT_TOOL, toolbar.back, id=_NTB2_BACK)
f.Bind(wx.EVT_TOOL, toolbar.zoom, id=_NTB2_ZOOM)
f.Bind(wx.EVT_TOOL, toolbar.pan, id=_NTB2_PAN)
f.Bind(wx.EVT_TOOL, toolbar.configure_subplot, id=_NTB2_SUBPLOT)
f.Bind(wx.EVT_TOOL, toolbar.save, id=_NTB2_SAVE)

tb.Realize()  

tb.Realize()
f.Show()
app.MainLoop()

This is a quick fix, but some one has to file a bug report for it I guess.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文