如何任意向 wxPythonsizer 添加一个项目?

How can I add an Item to wxPythonsizer arbitrarily?

我有一个 wxGridBagSizer,我希望稍后刷新这些项目。此代码无法将项目添加到 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()

记得在适当的 sizer 上使用 Layout,也许清除您正在重新加载的 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))
        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()