1

我已经为此苦苦挣扎了几天。如果我不使用 wx.Notebook,我可以让 FigureCanvas 在 sizer 中正确调整大小,但是一旦我将 FigureCanvas 放在 wx.Notebook 面板中,绘图似乎会忽略面板大小调整器。它也有某种刷新问题,如果你把它从屏幕上拉下来再打开,情节就消失了。也许命令的顺序有问题,但我似乎找不到。这篇文章:How to update a plot with python and Matplotlib帮助我做到了这一点,但我似乎无法弄清楚为什么一旦我在 wx.Notebook 中使用它时,情节的表现会有所不同。

这是它首次显示时的样子:http: //static.inky.ws/image/2205/image.jpg

这是在我中途关闭屏幕之后:http: //static.inky.ws/image/2206/image.jpg

#!/usr/bin/env python
import wx
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure

class MyFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self,
                          parent,
                          id=wx.ID_ANY,
                          pos=wx.DefaultPosition,
                          size=wx.Size( -1,-1 ),
                          style=wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL)

        fgSizer8 = wx.FlexGridSizer(0, 1, 0, 0)
        self.m_notebook2 = wx.Notebook(self,
                                       wx.ID_ANY,
                                       wx.DefaultPosition,
                                       wx.DefaultSize,
                                       0)

        self.m_panel8 = wx.Panel(self.m_notebook2,
                                 wx.ID_ANY,
                                 wx.DefaultPosition,
                                 wx.DefaultSize,
                                 wx.TAB_TRAVERSAL)

        bSizer20 = wx.BoxSizer(wx.VERTICAL)

        self.fig = Figure()
        self.axes = self.fig.add_subplot(111)
        self.pts = self.axes.plot([1, 2, 3], [4, 5, 7], 'ro-', picker=5)
        self.canvas = FigureCanvas(self, -1, self.fig)

        bSizer20.Add(self.canvas, 1, wx.ALL|wx.EXPAND, 5)

        self.m_panel8.SetSizer(bSizer20)
        self.m_panel8.Layout()
        bSizer20.Fit(self.m_panel8)
        self.m_notebook2.AddPage(self.m_panel8, u"a page", False)

        fgSizer8.Add(self.m_notebook2, 1, wx.EXPAND |wx.ALL, 5)

        self.SetSizer(fgSizer8)
        self.Layout()
        fgSizer8.Fit(self)

        self.Centre(wx.BOTH)

if __name__ == "__main__":
    app = wx.App(0)
    frame = MyFrame(None)
    frame.Show()
    app.MainLoop()
4

1 回答 1

3

改变

self.canvas = FigureCanvas(self, -1, self.fig)

self.canvas = FigureCanvas(self.m_panel8, -1, self.fig)

在此处输入图像描述

于 2012-06-22T12:57:17.490 回答