我在 wx.Frame(或 wx.Panel)的 Matplotlib Figure 画布中使用 Matplotlib NavigationToolbar2Wx 间歇性地遇到了这种行为。如果选择了缩放图标或平移图标,图标会消失,但在空白处单击仍会切换工具。Home、Backward 步骤或 Forward 步骤的图标都按预期运行。
任何人都可以就 1. 是什么原因和 2. 如何解决它提供建议吗?
感谢 joaquin 发布了稍微修改以包含工具栏的初始代码。(http://stackoverflow.com/questions/10737459/embedding-a-matplotlib-figure-inside-a-wxpython-panel)
我正在使用 python 2.6、wxPython 2.9.2.4 osx-carbon(经典)和 Matplotlib 1.1.0
谢谢
下面的代码显示了问题:
#!/usr/bin/env python
# encoding: UTF-8
"""
wxPython and Matplotlib Canvas with Matplotlib Toolbar.py
"""
from numpy import arange, sin, pi
import matplotlib
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
from matplotlib.figure import Figure
import wx
class CanvasPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.figure = Figure()
self.axes = self.figure.add_subplot(111)
self.canvas = FigureCanvas(self, -1, self.figure)
# Add Matplotlib Toolbar
# Add the Matplotlib Navigation toolBar here
self.toolbar=NavigationToolbar2Wx(self.canvas)
self.toolbar.AddLabelTool(5,'',wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN, wx.ART_TOOLBAR, (32,32)))
#self.Bind(wx.EVT_TOOL, self.NewTitle(), id=5)
self.toolbar.Realize()
# Add to Box Sizer
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.toolbar, 0, wx.LEFT | wx.TOP | wx.GROW)
self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
self.SetSizer(self.sizer)
self.Fit()
def draw(self):
t = arange(0.0, 3.0, 0.01)
s = sin(2 * pi * t)
self.axes.plot(t, s)
if __name__ == "__main__":
app = wx.PySimpleApp()
fr = wx.Frame(None, title='test',size=(800,600))
panel = CanvasPanel(fr)
panel.draw()
fr.Show()
app.MainLoop()