Search code examples
pythonwxpythonpanelwxwidgets

Force a panel to be square in wx (python)


Does anybody know how i would go about forcing a panel to be square.

The case where this occurs is have a panel in which I have a horizontal BoxSizer with 2 slots, In the left slot i have a panel that I am going to be drawing to though wx.PaintDC, on the right I am going to have a a list control or some other widget.

What i am trying to achieve is to have the window realizable and to have the left panel always stay square, and to have the right hand content fill the rest of the space.


Solution

  • One of the solutions is to use EVT_SIZE to respond to window resizing and update panel size in the event function. Simple example code:

    import wx
    from wx.lib.mixins.inspection import InspectionMixin
    
    class MyApp(wx.App, InspectionMixin):
        def OnInit(self):
            self.Init()  # initialize the inspection tool
            frame = wx.Frame(None)
            sizer = wx.BoxSizer(wx.HORIZONTAL)
            frame.SetSizer(sizer)
    
            self.__squarePanel = wx.Panel(frame)
            sizer.Add(self.__squarePanel, 0, wx.ALL | wx.EXPAND, 5)
    
            frame.Bind(wx.EVT_SIZE, self.OnSize)
    
            frame.Show()
            self.SetTopWindow(frame)
            return True  
    
        def OnSize(self, evt):
            frame = evt.GetEventObject()
            frameW, frameH = frame.GetSize()
            targetSide = min(frameW, frameH)
            self.__squarePanel.SetSize((targetSide, targetSide))
    
    
    app = MyApp()
    app.MainLoop()