Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wxPython, trying to removing all buttons from a sizer, always leaves one remaining

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.

like image 583
jmoggee Avatar asked Dec 11 '12 06:12

jmoggee


2 Answers

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.

like image 189
kraymer Avatar answered Nov 02 '22 06:11

kraymer


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.

like image 22
jmoggee Avatar answered Nov 02 '22 08:11

jmoggee