In the following code I try to delete two buttons - but one of them always stays.
The script creates following window:
Pressing ‘First’, ‘Second’ or ‘Third’ toggle button inside the first column will change the label of the button to ‘CLICKED’; clicking one of the toggle buttons on the second column will change the label to ‘PRESSED’:
Pressing ‘Add Infinity’ adds two more toggle buttons (with the label ‘Infinity’) to the end of the first / second column:
So far so good. However pressing ‘Delete Number’ will delete the first two toggle buttons (labeled as ‘First’):
BUT those buttons will not get removed as shown here:
For above screenshot I’ve enlarged the window horizontically. As shown the ‘First’ button (which is pressed) is behind the (unpressed) ‘Second’ button within the first column - and the second ‘First’ button is shown as well.
My environment:
Python 3.10.8 (main, Nov 1 2022, 14:18:21) [GCC 12.2.0] on linux
Type “help”, “copyright”, “credits” or “license” for more information.import wx
wx.version()
‘4.2.0 gtk3 (phoenix) wxWidgets 3.2.0’
And here the script:
#!/usr/bin/env python
import math
import time
import wx
class Example(wx.Frame):
def __init__(self, *args, **kw):
super(Example, self).__init__(*args, **kw)
self.SetTitle("Button Lists")
self.panel = wx.Panel(self)
hbox = wx.BoxSizer(wx.HORIZONTAL)
self.label_list = ('First','Second','Third')
self.button_scrollbox = wx.ScrolledWindow(self.panel, style=wx.VSCROLL)
self.button_scrollbox.SetScrollRate(10, 10)
self.button_scrollbox.EnableScrolling(True, True)
self.button_grid = wx.GridSizer(2)
self.first_button_list = []
self.second_button_list = []
for ix, param_name in enumerate(self.label_list):
self.first_button_list.append(wx.ToggleButton(self.button_scrollbox, label=param_name))
self.second_button_list.append(wx.ToggleButton(self.button_scrollbox, label=param_name))
self.button_grid.Add(self.first_button_list[ix])
self.button_grid.Add(self.second_button_list[ix])
self.first_button_list[ix].Bind(wx.EVT_TOGGLEBUTTON, self.on_button_click)
self.second_button_list[ix].Bind(wx.EVT_TOGGLEBUTTON, self.on_button_click)
add_button = wx.Button(self.button_scrollbox, label='Add Infinity')
self.button_grid.Add(add_button)
add_button.Bind(wx.EVT_BUTTON, self.on_add)
del_button = wx.Button(self.button_scrollbox, label='Delete Number')
self.button_grid.Add(del_button)
del_button.Bind(wx.EVT_BUTTON, self.on_del)
self.button_scrollbox.SetSizerAndFit(self.button_grid)
hbox.Add(self.button_scrollbox, 1, wx.ALIGN_TOP | wx.ALL, 0)
self.panel.SetSizerAndFit(hbox)
self.Layout()
self.Centre()
def on_button_click(self, event):
but_clicked = event.GetEventObject()
for ix in range(len(self.label_list)):
if but_clicked == self.first_button_list[ix]:
self.first_button_list[ix].SetLabel('CLICKED')
if not self.first_button_list[ix].GetValue():
self.first_button_list[ix].SetLabel(self.label_list[ix])
if but_clicked == self.second_button_list[ix]:
self.second_button_list[ix].SetLabel('PRESSED')
if not self.second_button_list[ix].GetValue():
self.second_button_list[ix].SetLabel(self.label_list[ix])
def on_add(self, event):
self.label_list = self.label_list + ('Infinity',)
self.first_button_list.append(wx.ToggleButton(self.button_scrollbox, label='Infinity'))
self.first_button_list[-1].SetLabel('Infinity')
self.first_button_list[-1].Bind(wx.EVT_TOGGLEBUTTON, self.on_button_click)
self.button_grid.Add(self.first_button_list[-1])
self.second_button_list.append(wx.ToggleButton(self.button_scrollbox, label='Infinity'))
self.second_button_list[-1].SetLabel('Infinity')
self.second_button_list[-1].Bind(wx.EVT_TOGGLEBUTTON, self.on_button_click)
self.button_grid.Add(self.second_button_list[-1])
self.button_scrollbox.SetSizerAndFit(self.button_grid)
self.button_scrollbox.SendSizeEventToParent()
def on_del(self, event):
print('deleting...')
print('Children button_scrollbox',len(self.button_scrollbox.GetChildren()))
self.label_list = self.label_list[1:]
self.first_button_list.remove(self.first_button_list[0])
self.second_button_list.remove(self.second_button_list[0])
# self.button_grid.Hide(0)
self.button_grid.Remove(0)
self.button_grid.Remove(0)
self.button_scrollbox.RemoveChild(self.button_scrollbox.GetChildren()[0])
self.button_scrollbox.RemoveChild(self.button_scrollbox.GetChildren()[0])
print('Children button_scrollbox',len(self.button_scrollbox.GetChildren()))
self.button_scrollbox.SendSizeEventToParent()
def main():
app = wx.App()
ex = Example(None)
ex.Show()
app.MainLoop()
if __name__ == '__main__':
main()
I’ve tried to remove the children from the sizer as well as from the window:
self.button_grid.Remove(0)
self.button_grid.Remove(0)
self.button_scrollbox.RemoveChild(self.button_scrollbox.GetChildren()[0])
self.button_scrollbox.RemoveChild(self.button_scrollbox.GetChildren()[0])
but without succes…
What’s wrong and how I can delete those buttons?
Many thanks in advance for any hints / comments,