How can I add an Item to wxPythonsizer arbitrarily?

I have a wxGridBagSizer and I wish to refresh the items later. This code fails to add the items to the sizer.

import wx
import random


class MainFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        super().__init__(None, *args, **kwargs)

        self.panel = MainPanel(self)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.panel)
        self.SetSizer(sizer)
        self.Center()
        self.Show()

    def on_reset_click(self, event):
        self.panel.numbers_fill(random.randint(1, 20))


class MainPanel(wx.Panel):
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)

        cmd_reset = wx.Button(self, id=wx.ID_OK)
        cmd_reset.Bind(wx.EVT_BUTTON, parent.on_reset_click)

        self.number_sizer = wx.GridBagSizer(5, 5)
        self.numbers_fill(random.randint(1, 20))

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(cmd_reset)
        sizer.Add(self.number_sizer)
        self.SetSizer(sizer)

    def numbers_fill(self, max_integer):
        self.clear_sizer(self.number_sizer)
        for index, _ in enumerate(range(max_integer)):
            row = int(index / 5)
            column = index % 5
            lbl_number = wx.StaticText(self, label=str(_))
            self.number_sizer.Add(lbl_number, pos=(row, column))

    @staticmethod
    def clear_sizer(sizer):
        for child in sizer.GetChildren():
            sizer_child = child.GetWindow()
            sizer_child.Hide()
            sizer.Detach(sizer_child)

if __name__ == '__main__':
        wx_app = wx.App()
        MainFrame()
        wx_app.MainLoop()

Answer

Remember to use Layout on the appropriate sizers and perhaps clearing the sizer you are re-loading may help.

import wx
import random


class MainFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        super().__init__(None, *args, **kwargs)

        self.panel = MainPanel(self)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.panel)
        self.SetSizer(sizer)
        self.Center()
        self.Show()

    def on_reset_click(self, event):
        self.panel.numbers_fill(random.randint(1, 20))


class MainPanel(wx.Panel):
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)

        cmd_reset = wx.Button(self, id=wx.ID_OK)
        cmd_reset.Bind(wx.EVT_BUTTON, parent.on_reset_click)

        self.number_sizer = wx.GridBagSizer(5, 5)
        self.numbers_fill(random.randint(1, 20))

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(cmd_reset)
        sizer.Add(self.number_sizer)
        self.SetSizer(sizer)

    def numbers_fill(self, max_integer):
        self.clear_sizer(self.number_sizer)
        for index, _ in enumerate(range(max_integer)):
            row = int(index / 5)
            column = index % 5
            lbl_number = wx.StaticText(self, label=str(_))
            self.number_sizer.Add(lbl_number, pos=(row, column))
        self.number_sizer.Layout()
        self.Layout()

    #@staticmethod
    def clear_sizer(self, sizer):
        for child in sizer.GetChildren():
            sizer_child = child.GetWindow()
            sizer_child.Hide()
            sizer.Detach(sizer_child)
        sizer.Clear()

if __name__ == '__main__':
        wx_app = wx.App()
        MainFrame()
        wx_app.MainLoop()