【问题标题】:wxpython + matplotlib: autoresizing a matplotlib figurewxpython + matplotlib:自动调整 matplotlib 图形的大小
【发布时间】:2023-03-21 03:08:01
【问题描述】:

这个 python 程序在 wxpython 窗口中绘制一个图形。

如何更改程序以便:

  • 当我调整窗口大小时,图形会调整大小
  • 不能将主窗口的大小调整为小于特定尺寸? (比如说,窗口默认大小的一半)

.

# adapted from:
# http://wiki.wxpython.org/Getting%20Started
# http://www.cs.colorado.edu/~kena/classes/5448/s11/presentations/pearse.pdf

import wx
import pylab as pl
import matplotlib
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas

class GUIPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.parent = parent

        # create some sizers
        sizer = wx.BoxSizer(wx.VERTICAL)

        # A button
        self.button =wx.Button(self, label="Tada!")
        self.Bind(wx.EVT_BUTTON, self.OnClick,self.button)

        # put up a figure
        self.figure = pl.figure()
        self.axes = self.drawplot(self.figure)
        self.canvas = FigureCanvas(self, -1, self.figure)

        sizer.Add(self.canvas, 0, wx.ALIGN_CENTER|wx.ALL)
        sizer.Add(self.button, 0, wx.ALIGN_CENTER|wx.ALL)

        self.SetSizerAndFit(sizer)  
    def log(self, fmt, *args):
        print (fmt % args)
    def OnClick(self,event):
        self.log("button clicked, id#%d\n", event.GetId())
    def drawplot(self, fig):
        ax = fig.add_subplot(1,1,1)
        t = pl.arange(0,1,0.001)
        ax.plot(t,t*t)
        ax.grid()
        return ax

app = wx.App(False)
frame = wx.Frame(None)
panel = GUIPanel(frame)
frame.Fit()
frame.Center()
frame.Show()
app.MainLoop()

【问题讨论】:

  • 是否会根据您的其他问题而改变?
  • 没有。 [填空字符的无用词]

标签: python layout matplotlib wxpython


【解决方案1】:

1) 将您设置 sizer 的方式修改为:

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

这里是method reference。安装和使用wxPython Demo 也很有帮助。 Sizer 很好地覆盖在那里。顺便说一句:wx.ALL 没有用,除非你指定边框。


2) 并在frame.Show() 之后将其添加到您的框架设置中:

size = frame.GetSize()
frame.SetMinSize((size[0] / 2, size[1] / 2))

【讨论】:

    猜你喜欢
    • 2010-11-19
    • 1970-01-01
    • 2013-09-17
    • 1970-01-01
    • 2015-03-08
    • 1970-01-01
    • 1970-01-01
    • 2015-04-01
    • 2021-10-06
    相关资源
    最近更新 更多