Search code examples
wxpythonwxwidgets

How do I correctly update several levels of nested sizers when some inner content changes its size?


I’m building a WX app in which the main window (“frame”) has two content areas: a main content area on the left and a sidebar on the right. The sidebar consists of a vertically-stacked collection of panels, and each of these panels consists of a title and a content area. The main window’s view hierarchy looks like this:

  • Main window (wx.BoxSizer, horizontal)
    • Main content area (wx.Panel)
    • Sidebar (wx.BoxSizer, vertical)
      • Sidebar panel 1 (wx.BoxSizer, vertical)
        • Title 1 (wx.StaticText)
        • Content area 1 (wx.Panel)
      • Sidebar panel 2 (wx.BoxSizer, vertical)
        • Title 2 (wx.StaticText)
        • Content area 2 (wx.Panel)

The content area in each sidebar panel will be changing as my application runs, so the sidebar panels need to be able to resize themselves as their content areas grow and shrink, and likewise the sidebar itself needs to be able to move its panels up and down appropriately as its siblings grow and shrink.

My question is how to make sure that all of these sizers are updating appropriately. For example, “sidebar panel 2” is its own class. If it does something that changes the size of its content area, which sizers does it need to update, and which method should it use to do so? And which sizers should just update automatically?

(I ask because I’m currently having a bear of a time getting the content area of one of these sidebar panels to update correctly, and I’d like to make sure that everything above it in the view hierarchy is correct before I go rewriting the class.)


Solution

  • You'll have to call Layout() on any widget or the sizer that the widget is in. This has actually been explained quite well by Robin Dunn (creator of wxPython) on the wxPython wiki here:

    It mentions that you can use the Widget Inspection Tool to help you figure out what widget/sizer to call Layout on.