You should first test that the button is active, as you may be trying to delete a button that doesn't exits. So, a dictionary to store the button makes sense as you can look it up before trying to delete.
import wx
class Test:
def __init__(self):
app = wx.App(False)
frame = wx.Frame(None, wx.ID_ANY, "Test App")
panel = wx.Panel(frame)
vboxsizer = wx.BoxSizer(wx.VERTICAL)
self.grid = wx.GridSizer(rows=0, cols=3)
self.button_dict = {}
for j in range(5):
b = wx.Button(panel, label="Remove Button %d" % (j), id=j)
self.grid.Add(b)
b.Bind(wx.EVT_BUTTON, self.remove_button)
self.button_dict[j] = b
vboxsizer.Add(self.grid, 1, border=2, flag=wx.EXPAND|wx.ALL)
panel.SetSizer(vboxsizer)
frame.Show(True)
app.MainLoop()
def remove_button(self, e):
button_num = e.GetId()
print "ID =", button_num, type(button_num)
if button_num in self.button_dict:
self.button_dict[button_num].Hide()
if __name__ == "__main__":
t = Test()