gpt4 book ai didi

python - wxpython + matplotlib : autoresizing a matplotlib figure

转载 作者:行者123 更新时间:2023-12-01 05:46:32 26 4
gpt4 key购买 nike

这个 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()

最佳答案

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

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

这里是method reference 。安装和使用wxPython Demo也很有帮助。尺寸测量器在那里被很好地覆盖。顺便说一句:除非您指定边框,否则 wx.ALL 是无用的。

<小时/>

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

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

关于python - wxpython + matplotlib : autoresizing a matplotlib figure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15930189/

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