wxPython - 在ScrolledPanel中调整MatPlotLib FigureCanvas的大小

时间:2010-11-11 16:51:15

标签: python wxpython matplotlib

我在Panel的ScrolledPanel中有一个数字画布。我想改变图画布的大小。例如。

mplFigure.set_figheight(1.0)
someobject.soSomethingThatResizeItAll

我该怎么做?

由于

大卫

这是我的构造代码。

    panel = wx.Panel(self)      # we put the scrollablePanel in the panel so later on we can do fit to window sizing too (i.e. by removing the scrollablePanel)

    # create a scrollablePanel to hold the canvas        
    scrollablePanel = ScrolledPanel(parent=panel, id=wx.ID_ANY, name="scrolledPanel", style=wx.ALWAYS_SHOW_SB)
    scrollablePanel.SetupScrolling()
    scrollablePanel.SetBackgroundColour(wx.Colour(128,128,128))

    # create mpl canvas and figure
    mplFigure = Figure(figsize=A6H, facecolor="white") #, edgecolor="black")
    mplFigureCanvas = FigureCanvasWxAgg(parent=scrollablePanel, id=wx.ID_ANY, figure=mplFigure)
    #mplFigureCanvas.SetWindowStyle=wx.SIMPLE_BORDER     # not sure if this will have any affect?
    #mplFigureCanvas.SetBackgroundColour(wx.Colour(0,0,0))

    # center the FigureCanvas inthe scrollablePanel
    sizer1 = wx.BoxSizer(wx.VERTICAL)
    sizer1.Add(mplFigureCanvas, proportion=0, flag=wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, border=8)
    sizer2 = wx.BoxSizer(wx.HORIZONTAL)
    sizer2.Add(sizer1, proportion=1, flag=wx.ALIGN_CENTER_VERTICAL)
    scrollablePanel.SetSizer(sizer2)

    # create mpl toolbar
    #mplToolbar = NavigationToolbar2Wx(mplFigureCanvas)
    #mplToolbar.Realize()                          # needed to support Windows systems

    # use another sizer to add the scrollablePanel to the main panel
    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(scrollablePanel, 1, wx.LEFT | wx.EXPAND)

    #sizer.Add(mplToolbar, 0, wx.LEFT | wx.EXPAND)
    #mplToolbar.Show()

    panel.SetSizer(sizer)

3 个答案:

答案 0 :(得分:0)

我找到了部分答案。

mplFigureCanvas.SetSize(...)最初会这样做,但是一旦我调整框架大小,它就会回到原来的大小。

- DB

答案 1 :(得分:0)

好吧,我现在有一些可怕的代码可以获得结果,但它并不漂亮。

    mplFigure.set_size_inches(sizeInInches)
    l,b,w,h = mplFigure.bbox.bounds
    w = int(math.ceil(w))
    h = int(math.ceil(h))
    mplCanvas.SetInitialSize(size=wx.Size(w, h))
    size = panel.Size
    panel.SetSize(wx.Size(size.x, size.y-1))
    panel.SetSize(wx.Size(size.x, size.y))

欢迎改进。

- DB

答案 2 :(得分:0)

如果我理解你的问题,我认为你会想要以不同的方式设置你的面板。我将mpl_canvas放在wx.Panel中,然后将该面板放入ScrolledPanel。然后放大/缩小画布只需更新面板的MinSize(panel.SetMinSize())。