1

This python program plots a figure in a wxpython window.

How can I change the program so that:

  • the figure resizes when I resize the window
  • the main window cannot be resized smaller than a particular dimension? (say, half the default size of the window)

.

# 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()
2
  • does this change in light of your other question? Commented Apr 11, 2013 at 5:04
  • no. [GRATUITOUS USELESS WORDS TO FILL UP CHARACTERS] Commented Apr 11, 2013 at 5:10

1 Answer 1

2

1) Modify the way you setup your sizer to this:

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

Here is method reference. It is also helpful to install and use wxPython Demo. Sizers are covered nicely there. BTW: wx.ALL is useless unless you specify border.


2) And add this to your frame setup after frame.Show():

size = frame.GetSize()
frame.SetMinSize((size[0] / 2, size[1] / 2))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.