使用平移工具时,嵌入在wxPython中的Matplotlib不会刷新

时间:2014-02-27 23:24:45

标签: python macos matplotlib wxpython

我正在编写一个带有嵌入wxPython(2.8.10.1)GUI的matplotlib(1.3.1)图形的应用程序,用于分析光谱。我有一个NavigationToolbar2WxAgg,所以我可以平移和缩放图。

当我使用平移工具时,图形不会刷新...直到我手动调整整个窗口的大小,表面上是因为它会触发Canvas上的setSize调用,然后刷新它。

作为临时修复,我实现了一个50毫秒的计时器,我在其中调用self.Canvas.Refresh()。非常不理想......

由于我刚刚熟悉wxPython,我不清楚我的事件管道在哪里被破坏,并想知道那里的任何向导是否可能知道我需要阅读哪些文档,或者我需要哪个事件/绑定学习。

非常感谢!

扎克

源代码:

#!/usr/bin/env python 
# -*- coding: US-ASCII -*- 
# 
# generated by wxGlade 0.6.8 on Thu Feb 27 11:09:38 2014 
# 

import wx 
import matplotlib 
matplotlib.use('WXAgg') 
from matplotlib.figure import Figure as fig 
from numpy import * 
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg 
from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg 

# begin wxGlade: dependencies 
import gettext 
# end wxGlade 

# begin wxGlade: extracode 
# end wxGlade 

TIMER_ID = 1000 

class EDSLiveViewFrame(wx.Frame): 
    def __init__(self, *args, **kwds): 
        # begin wxGlade: EDSLiveViewFrame.__init__ 
        kwds["style"] = wx.DEFAULT_FRAME_STYLE 
        wx.Frame.__init__(self, *args, **kwds) 
        self.SpectrumPanel = wx.Panel(self, wx.ID_ANY, style=wx.TAB_TRAVERSAL | wx.FULL_REPAINT_ON_RESIZE) 
        self.FigurePanel = wx.Panel(self.SpectrumPanel, wx.ID_ANY) 
        self.btnLogarithmic = wx.ToggleButton(self.SpectrumPanel, wx.ID_ANY, _("Logarithmic")) 
        self.notebookSettings = wx.Notebook(self, wx.ID_ANY, style=0) 
        self.InstrumentPane = wx.Panel(self.notebookSettings, wx.ID_ANY) 
        self.SamplePane = wx.Panel(self.notebookSettings, wx.ID_ANY) 

        self.__set_properties() 
        self.__do_layout() 

        self.Bind(wx.EVT_TOGGLEBUTTON, self.OnLogPress, self.btnLogarithmic) 
        # end wxGlade 

        self.InitMatplotlibFigure() 

        self.DrawFigure() 
        self.Layout() 

        self.Bind(wx.EVT_SIZE, self.OnSize, self) 

        self.timer = wx.Timer(self, TIMER_ID) 
        self.timer.Start(50) 
        wx.EVT_TIMER(self, TIMER_ID, self.OnTimer) 

    def __set_properties(self): 
        # begin wxGlade: EDSLiveViewFrame.__set_properties 
        self.SetTitle(_("frame_1")) 
        self.SetSize((998, 652)) 
        self.btnLogarithmic.SetMinSize((100, 100)) 
        self.notebookSettings.SetMinSize((-1, 300)) 
        # end wxGlade 

    def __do_layout(self): 
        # begin wxGlade: EDSLiveViewFrame.__do_layout 
        sizer_1 = wx.BoxSizer(wx.VERTICAL) 
        SpectrumSizer = wx.FlexGridSizer(1, 2, 0, 0) 
        FigureSizer = wx.BoxSizer(wx.VERTICAL) 
        self.FigurePanel.SetSizer(FigureSizer) 
        SpectrumSizer.Add(self.FigurePanel, 1, wx.EXPAND, 0) 
        SpectrumSizer.Add(self.btnLogarithmic, 0, wx.FIXED_MINSIZE, 0) 
        self.SpectrumPanel.SetSizer(SpectrumSizer) 
        SpectrumSizer.AddGrowableRow(0) 
        SpectrumSizer.AddGrowableCol(0) 
        sizer_1.Add(self.SpectrumPanel, 1, wx.ALL | wx.EXPAND, 2) 
        self.notebookSettings.AddPage(self.InstrumentPane, _("Instrument")) 
        self.notebookSettings.AddPage(self.SamplePane, _("Sample")) 
        sizer_1.Add(self.notebookSettings, 0, wx.EXPAND | wx.ALIGN_BOTTOM, 0) 
        self.SetSizer(sizer_1) 
        self.Layout() 
        # end wxGlade 

    def OnSize(self, e): 
        self.Canvas.SetSize(self.FigurePanel.GetSize()) 
        e.Skip() 

    def OnTimer(self, e): 
        self.Canvas.Refresh() 

    def InitMatplotlibFigure(self): 
        # The figure goes in a FigureCanvas which fills a wxWidgets panel. 
        self.dpi = 100 
        self.Figure = fig((5.0, 4.0), dpi=self.dpi) 
        self.Canvas = FigureCanvasWxAgg(self.FigurePanel, wx.ID_ANY, self.Figure) 
        self.axes = self.Figure.add_subplot(111) 
        self.PlotToolbar = NavigationToolbar2WxAgg(self.Canvas) 
        self.FigurePanel.GetSizer().Add(self.Canvas, 1, flag=wx.ALL|wx.EXPAND) 
        self.FigurePanel.GetSizer().Add(self.PlotToolbar, 0, wx.EXPAND) 

    def DrawFigure(self): 
        self.x = arange(0,10, 0.01) 
        self.y = sin(self.x) 
        self.axes.clear() 
        self.axes.grid(True) 

        if self.btnLogarithmic.GetValue() == True: 
            self.axes.semilogy(self.x,self.y) 
        else: 
            self.axes.plot(self.x,self.y) 

        self.Canvas.draw() 
        self.Canvas.Refresh() 


    def OnLogPress(self, event):  # wxGlade: EDSLiveViewFrame.<event_handler> 
        self.DrawFigure() 
        event.Skip() 
# end of class EDSLiveViewFrame 


class MyApp(wx.App): 
    def OnInit(self): 
        wx.InitAllImageHandlers() 
        MainFrame = EDSLiveViewFrame(None, wx.ID_ANY, "") 
        self.SetTopWindow(MainFrame) 
        MainFrame.Show() 
        return 1 

# end of class MyApp 

if __name__ == "__main__": 
    gettext.install("app") # replace with the appropriate catalog name 

    app = MyApp(0) 
    app.MainLoop() 

0 个答案:

没有答案