我试图使matplotlib与wxPython合作,我的下面列出的应用程序的行为是尴尬的:在Ubuntu下它的工作原理,但在Windows XP似乎冻结,即button不响应点击,元素是重叠的GridSizer没有工作)。 我究竟做错了什么?
更新:将GridSizer更改为BoxSizer后,在两种操作系统下都可以正常工作,但GridSizer的问题仍未解决。
# wxPython module import wx # Matplotlib Figure object from matplotlib.figure import Figure # Numpy functions for image creation import numpy as np # import the WxAgg FigureCanvas object, that binds Figure to # WxAgg backend. In this case, this is a wxPanel from matplotlib.backends.backend_wxagg import \ FigureCanvasWxAgg as FigureCanvas class MyFigurePanel(wx.Panel): """Class to represent a Matplotlib Figure as a wxFrame""" def __init__(self, parent): wx.Panel.__init__(self, parent) # usual Matplotlib functions self.figure = Figure()#figsize=(6, 4), dpi=100) self.axes = self.figure.add_subplot(111) x = np.arange(0, 6, .01) y = np.sin(x**2)*np.exp(-x) self.axes.plot(x, y) # initialize the FigureCanvas, mapping the figure to # the Wx backend self.canvas = FigureCanvas(self, wx.ID_ANY, self.figure) class MyButtonPanel(wx.Panel): def __init__(self, parent): wx.Panel.__init__(self, parent) save_button = wx.Button(self, label = 'SAVE se') class MyFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self,None) panelx = wx.Panel(self) self.figure = MyFigurePanel(panelx) self.buttons = MyButtonPanel(panelx) grid = wx.GridSizer(1,2) grid.Add(self.figure) grid.Add(self.buttons) panelx.SetSizer(grid) # Create a wrapper wxWidgets application app = wx.PySimpleApp() # instantiate the Matplotlib wxFrame frame = MyFrame() # show it frame.Show(True) # start wxWidgets mainloop app.MainLoop()
这工作(修改后的MyFrame
):
from matplotlib.backends.backend_wxagg import \ FigureCanvasWxAgg as FigureCanvas class MyFigurePanel(wx.Panel): """Class to represent a Matplotlib Figure as a wxFrame""" def __init__(self, parent): wx.Panel.__init__(self, parent) self.figure = Figure()#figsize=(6, 4), dpi=100) self.axes = self.figure.add_subplot(111) x = np.arange(0, 6, .01) y = np.sin(x**2)*np.exp(-x) self.axes.plot(x, y) self.canvas = FigureCanvas(self, wx.ID_ANY, self.figure) class MyButtonPanel(wx.Panel): def __init__(self, parent): wx.Panel.__init__(self, parent) button = wx.Button(self, label = 'SAVE se') class MyFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self, None) self.figure = MyFigurePanel(self) self.buttons = MyButtonPanel(self) grid = wx.BoxSizer(wx.VERTICAL) grid.Add(self.buttons, flag=wx.EXPAND) grid.Add(self.figure, flag=wx.EXPAND) self.SetSizer(grid) self.Fit() # Create a wrapper wxWidgets application app = wx.PySimpleApp() # instantiate the Matplotlib wxFrame frame = MyFrame() # show it frame.Show(True) # start wxWidgets mainloop app.MainLoop()
使其运行的关键变化是消除了MyFrame
中的面板。 其实你已经添加了两个面板。 此外,我使用GridSizer
wx.BoxSizer()
更好看( GridSizer
生成相同大小的单元格)