I have been struggling with this for a while now. I have created a simple GUI,
this is what I am trying to achieve.
When the user clicks "Buttons 1", 3 buttons, "A", "B" and "C" appear to the
left. When the user clicks "Buttons 2", those 3 buttons are removed and
replaced with buttons "C", "D" and "E".
I've tried everything I can think of but I don't seem to able to successfully
remove the old buttons before sticking the new ones in. Can anyone help?
I am running python version 2.4.2 on Ubuntu Linux (Breezy), the code is below:
#!/usr/bin/python
import wx
class Example(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
topsizer = wx.BoxSizer(wx.VERTICAL)
title = wx.StaticText(self, -1, "Example")
topsizer.Add(title, 0, wx.ALIGN_CENTER_HORIZONTAL)
lefttorightsizer = wx.BoxSizer(wx.HORIZONTAL)
leftsizer = wx.BoxSizer(wx.VERTICAL)
typelabel = wx.StaticText(self, -1, "Buttons: ")
leftsizer.Add(typelabel, 0, wx.ALIGN_LEFT)
self.typesizer = wx.BoxSizer(wx.VERTICAL)
leftsizer.Add(self.typesizer, 1, wx.EXPAND)
lefttorightsizer.Add(leftsizer, 2, wx.EXPAND)
rightsizer = wx.BoxSizer(wx.VERTICAL)
receiptbuttonssizer = wx.BoxSizer(wx.VERTICAL)
buttons1button = wx.Button(self, -1, "Buttons 1")
buttons1button.Bind(wx.EVT_BUTTON, self.Buttons1)
receiptbuttonssizer.Add(buttons1button, 1, wx.EXPAND)
buttons2button = wx.Button(self, -2, "Buttons 2")
buttons2button.Bind(wx.EVT_BUTTON, self.Buttons2)
receiptbuttonssizer.Add(buttons2button, 1, wx.EXPAND)
rightsizer.Add(receiptbuttonssizer, 0, wx.EXPAND)
lefttorightsizer.Add(rightsizer, 1, wx.EXPAND)
topsizer.Add(lefttorightsizer, 1, wx.EXPAND)
self.SetSizer(topsizer)
def Buttons1(self, ID):
frame = ID.GetEventObject().GetGrandParent()
parent = ID.GetEventObject().GetParent()
#typechildren = self.typesizer.GetChildren()
#print "typechildren = " + str(typechildren)
while len(self.typesizer.GetChildren()) > 1:
self.typesizer.GetChildren()[0].Destroy()
#self.typesizer.Remove(0)
typesizer = wx.BoxSizer(wx.HORIZONTAL)
self.abutton = wx.Button(self, -1, "A")
typesizer.Add(self.abutton, 1, wx.EXPAND)
self.bbutton = wx.Button(self, -1, "B")
typesizer.Add(self.bbutton, 1, wx.EXPAND)
self.cbutton = wx.Button(self, -1, "C")
typesizer.Add(self.cbutton, 1, wx.EXPAND)
self.typesizer.Add(typesizer, 1, 1, wx.EXPAND)
self.Layout()
def Buttons2(self, ID):
frame = ID.GetEventObject().GetGrandParent()
parent = ID.GetEventObject().GetParent()
typechildren = self.typesizer.GetChildren()
print "typechildren = " + str(typechildren)
while len(typechildren) > 1:
widget = self.typesizer.GetChildren()[0]
self.typesizer.Remove(0)
#print "Widget Removed"
widget.Destroy()
#print "Widget Destroyed"
typesizer = wx.BoxSizer(wx.HORIZONTAL)
self.dbutton = wx.Button(self, -1, "D")
typesizer.Add(self.dbutton, 1, wx.EXPAND)
self.ebutton = wx.Button(self, -1, "E")
typesizer.Add(self.ebutton, 1, wx.EXPAND)
self.fbutton = wx.Button(self, -1, "F")
typesizer.Add(self.fbutton, 1, wx.EXPAND)
self.typesizer.Add(typesizer, 1, 1, wx.EXPAND)
self.Layout()
app = wx.App()
frame = wx.Frame(None, -1, "My Frame")
frame.Show()
Example(frame)
app.MainLoop()
Hope you can help
Adam.