gpt4 book ai didi

python - 使用 wxPython 和 matplotlib 的简单应用程序卡住

转载 作者:可可西里 更新时间:2023-11-01 14:15:14 24 4
gpt4 key购买 nike

我试图让 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()

最佳答案

这有效(在修改 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 生成相同大小的单元格)

关于python - 使用 wxPython 和 matplotlib 的简单应用程序卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6836274/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com