I have the following code to add 6 buttons to a BoxSizer
for word in words:
btn = wx.Button(self, label=word)
btn.Bind(wx.EVT_BUTTON, self.onWordSelect)
In my onWordSelect
method I'm trying to remove all the buttons I've created on the Sizer, so that I can recreate new buttons. My problem is that all the buttons gets removed except for the last one.
Here is my code for removing the buttons:
for child in self.sizer.GetChildren():
self.sizer.Remove(child.Window)
self.sizer.Layout()
When checking len(self.sizer.GetChildren())
it returns 0
, but the last button is still visible on the screen.
From http://wxpython.org/docs/api/wx.Sizer-class.html#Remove :
For historical reasons calling this method with a wx.Window parameter is depreacted, as it will not be able to destroy the window since it is owned by its parent. You should use Detach instead.
You removed elements from the sizer but they still exist, being printed one over the other: add one line in your loop to destroy
or hide
them and it should be good.
After doing a bit of delving through the documentation I've found that the easiest way of removing all the controls is to use self.sizer.DeleteWindows()
instead of removing each Button individually in a loop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With