使用wxPython和matplotlib的简单应用程序冻结

4 投票
1 回答
1548 浏览
提问于 2025-04-16 22:19

我正在尝试让matplotlib和wxPython一起工作,但我下面的应用程序表现得很奇怪:在Ubuntu系统下运行正常,但在Windows XP下似乎卡住了,也就是说按钮点击没有反应,而且界面元素重叠(就像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()

1 个回答

3

这个方法可以用(在修改了 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 中的面板。其实你已经添加了两个面板了。还有,我使用了 wx.BoxSizer() 来让界面看起来更好(GridSizer 会产生大小相同的单元格)。

撰写回答