Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing a wxPython Window

Is it possible to make a wxPython window only re-sizable to a certain ratio? I know you can disable resizing; however, I'd like it so when the window was resized it stuck to a certain width to height ratio.

like image 462
rectangletangle Avatar asked May 15 '11 01:05

rectangletangle


1 Answers

One obvious way to do this would be to bind wx.EVT_SIZE to a function that constrains the aspect ratio. I'm not certain this is The Right Way to do this, but it works:

import wx

class SizeEvent(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)

        self.Bind(wx.EVT_SIZE, self.OnSize)
        self.Centre()
        self.SetSizeWH(400, 300)
        self.Show(True)

    def OnSize(self, event):
        hsize = event.GetSize()[0] * 0.75
        self.SetSizeHints(minW=-1, minH=hsize, maxH=hsize)
        self.SetTitle(str(event.GetSize()))

app = wx.App()
SizeEvent(None, 1, 'sizeevent.py')
app.MainLoop()

(The boilerplate is borrowed from here.)

like image 63
senderle Avatar answered Sep 29 '22 09:09

senderle