Au lieu de item.Show(False)
qui cache simplement l'élément, vous devez utiliser item.Destroy()
qui l'enlève.
Plus tard, vous pouvez self.activetoolsizer.Add(......)
à cette position et souvenez-vous d'utiliser self.activetoolsizer.Layout()
exemple Silly (garder cliquant sur le bouton 1 et le bouton 2 sera retiré et remplacé):
import wx
class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title)
self.button1 = wx.Button(self,-1, "Button 1")
self.button2 = wx.Button(self,-1, "Button 2")
self.sizer = wx.GridBagSizer(2, 2)
self.sizer.Add(wx.StaticText(self,-1, "Label 1"), (0, 0), flag=wx.ALIGN_CENTER)
self.sizer.Add(self.button1, (0, 1), flag=wx.EXPAND)
self.sizer.Add(self.button2, (1, 0), flag=wx.EXPAND)
self.sizer.Add (wx.StaticText(self,-1, "Label 2"), (1, 1), flag=wx.ALIGN_CENTER)
self.Bind(wx.EVT_BUTTON, self.button_click, self.button1)
self.SetSizerAndFit(self.sizer)
self.button_index = 3
def button_click(self, event):
item = self.sizer.FindItemAtPosition((1, 0))
if (item != None):
self.button2.Destroy()
else:
self.button2 = wx.Button(self,-1, "Button "+str(self.button_index))
self.sizer.Add(self.button2, (1, 0), flag=wx.EXPAND)
self.sizer.Layout()
self.button_index +=1
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, -1, "Replace item in Gridbagsizer")
frame.Show(True)
return True
app = MyApp()
app.MainLoop()