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

发布于 2024-10-01 15:08:51 字数 1786 浏览 2 评论 0原文

我在面板的 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)

I have a figure canvas in a ScrolledPanel in a Panel. I want to change the size of the figure canvas. E.g.

mplFigure.set_figheight(1.0)
someobject.soSomethingThatResizeItAll

How can I do this?

Thanks

David

Here's my construction code.

    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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

盗心人 2024-10-08 15:08:51

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

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

如有改进将受到欢迎。

——数据库

Well I now have some horrible code that gets the result but it's not pretty.

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

An improvement would be welcome.

-- DB

2024-10-08 15:08:51

好吧,我已经找到了部分答案。

mplFigureCanvas.SetSize(...) 最初会执行此操作,但一旦我调整框架大小,它就会恢复到原始大小。

——数据库

Well I've found part of the answer.

mplFigureCanvas.SetSize(...) does it initially but as soon as I resize the frame it goes back to the original size.

-- DB

楠木可依 2024-10-08 15:08:51

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

If I understand your question correctly I think you'll want to set up your panels differently. I would put the mpl_canvas in a wx.Panel and then put that panel into the ScrolledPanel. Then to enlarge/shrink the canvas just update the MinSize of the panel (panel.SetMinSize()).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文