开发者

simple application using wxPython and matplotlib freezes

I'm trying to make matplotlib cooperate with wxPython,开发者_如何学Python and my application listed below behaves awkward: under Ubuntu it works as intended, but under Windows XP it seems to freeze, i.e. button does not react to clicking, and elements are overlapping(as if GridSizer didn't work). What am I doing wrong?

Update: After changing GridSizer to BoxSizer everything works under both operating systems, but problem with GridSizer is still not solved.

# 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()


This works (after modifying 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()

The key change to make it run is the elimination of the panel in MyFrame. Actually you are already adding two panels. Also I use a wx.BoxSizer() for a better look (GridSizer produces cells of the same size)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜