Search code examples
pythonwxpythonwxwidgets

Make a wxPanel resizable by the user


I have a wxPanel. How can I make it resizable by the user? With a drag-and-drop resize bar?

I wonder what the widgets I need are.

Thank you.


Solution

  • You can use wx.SplitterWindow to make panels in a frame to be resizable (that is to divide a frame in, for example, two resizable sub-windows).

    If what you want is to be able to resize a panel on a grid of other panels then you can use wx.lib.resizewidget. This provides of a drag-and-drop like bar but you can not move the position of the widget only resize it.

    You have examples of both in the wxpython docs and demos package

    Here you have an example using a SplitterWindow (the code is very verbose because was autogenerated with wxglade. It can be simplified if you want to):

    import wx  
    
    class MyFrame(wx.Frame):
        def __init__(self, *args, **kwds):
            kwds["style"] = wx.DEFAULT_FRAME_STYLE
            wx.Frame.__init__(self, *args, **kwds)
            self.window_1 = wx.SplitterWindow(self, wx.ID_ANY, style=wx.SP_3D | wx.SP_BORDER)
            self.window_1_pane_1 = wx.Panel(self.window_1, wx.ID_ANY)
            self.window_1_pane_2 = wx.Panel(self.window_1, wx.ID_ANY)
    
            self.__set_properties()
            self.__do_layout()
    
        def __set_properties(self):
            self.SetTitle("frame_1")
            self.window_1_pane_1.SetBackgroundColour(wx.Colour(255, 255, 0))
            self.window_1_pane_2.SetBackgroundColour(wx.Colour(50, 153, 204))
    
        def __do_layout(self):
            sizer_1 = wx.BoxSizer(wx.VERTICAL)
            self.window_1.SplitVertically(self.window_1_pane_1, self.window_1_pane_2)
            sizer_1.Add(self.window_1, 1, wx.EXPAND, 0)
            self.SetSizer(sizer_1)
            sizer_1.Fit(self)
            self.Layout()
    
    if __name__ == "__main__":
        app = wx.PySimpleApp(0)
        wx.InitAllImageHandlers()
        frame_1 = MyFrame(None, wx.ID_ANY, "")
        app.SetTopWindow(frame_1)
        frame_1.Show()
        app.MainLoop()
    

    enter image description here