user1594322
user1594322

Reputation: 2048

wxPython keeping a wx.Frame square?

I'm wanting to keep a Frame square (like a chess board). It can be resized. Here is what I tried. It keeps the Frame square, but it won't decrease in size. It will only increase in size.

import wx

class MainWindow (wx.Frame):
    def __init__ (self):
        wx.Frame.__init__(self, None)
        self.Bind(wx.EVT_SIZE, self.OnSize)
        self.Show()

    def OnSize (self, event):
        w,h = self.GetClientSize()
        size = max(w,h)
        self.SetClientSize((size,size))

if __name__ == '__main__':
    app = wx.PySimpleApp()
    win = MainWindow()
    app.MainLoop()

Upvotes: 2

Views: 135

Answers (1)

Bouke
Bouke

Reputation: 12138

(Copied from my comment as suggested by RobinDunn)

The problem is that max(w, h) will always return the largest number. So if you are decreasing the size horizontally, the vertical size will overrule the horizontal size. So ideally, you would want to know what changed in OnSize, so you can deduce what the user intent was (increase or decrease), and use min/max accordingly. One idea is to store the previous size on the frame, so you can compare the new and previous size.

The idea is something like the code below. Note that it is still rough around the edges, but it should give you a starting point.

import wx


class MainWindow(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        self._size = self.ClientSize
        self.Bind(wx.EVT_SIZE, self.OnSize)
        self.Show()

    def OnSize(self, event):
        if self._size[0] != self.ClientSize[0]:
            self._size = self.ClientSize[0], self.ClientSize[0]
        elif self._size[1] != self.ClientSize[1]:
            self._size = self.ClientSize[1], self.ClientSize[1]

        if self._size != self.ClientSize:
            self.ClientSize = self._size


if __name__ == '__main__':
    app = wx.PySimpleApp()
    win = MainWindow()
    app.MainLoop()

Upvotes: 1

Related Questions