2007/1/25, Eli Golovinsky <gooli@tuzig.com>:
> I would say that you sizerItem got deleted (in the C++ sense) by Detach.
>
> The docs say that Detach doesn't destroy the window, but I imagine it
> does destroy the sizer item, assuming (usually correctly) that the sizer
> owns its sizer items.
>
> In order to do what you want, you'll have to create a new sizer item and
> copy all the data from the old sizer item to the new one before
> Detach-ing it. I don't know of any way to clone a SizerItem
> automatically, so you'll probably have to copy all the parameters yourself.
>
> Luca Politti wrote:
>
> > Hi, I have a problem with the wxSizer AddItem method:
> > when I try to add a wxSizerItem with the AddItem method, the
> > application crashes.
> > Here I put the error example code:
> >
> > import wx
> >
> > class problem(wx.Panel):
> > def __init__(self,parent):
> > super(problem,self).__init__(parent,-1)
> >
> > # creates the items
> > _lbl_l = wx.StaticText(self,-1,"Label")
> > _tctrl_t = wx.TextCtrl(self,-1)
> > _btn_b = wx.Button(self,-1,"problem launch")
> >
> > # button event handling
> > _btn_b.Bind(wx.EVT_BUTTON,self.OnButton)
> >
> > # adds the items to the sizer
> > self.sizer = wx.BoxSizer(wx.VERTICAL)
> > self.sizer.AddMany([ (_lbl_l,0,wx.ALL|wx.ALIGN_LEFT,10),
> > (_tctrl_t,0,wx.ALL|wx.ALIGN_LEFT,10),
> > (_btn_b,0,wx.ALL|wx.ALIGN_LEFT,10) ])
> >
> > # Optimize layout
> > self.SetSizerAndFit(self.sizer)
> > self.SetSize(self.sizer.GetSize())
> >
> > def OnButton(self,evt):
> > # ------- the problem -------
> > sizerItem = self.sizer.GetItem(0)
> > self.sizer.Detach(0)
> > self.sizer.AddItem(sizerItem)
> > # ---------------------------
> >
> > app = wx.PySimpleApp()
> > frame = wx.Frame(None,-1,"frame")
> > problem(frame)
> > frame.Show()
> > app.MainLoop()
> >
> > Can you help me please?
> > Thanks
> >
> > Luca Politti
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
> > For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
> For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org
>
Try this:
import wx
class problem(wx.Panel):
def __init__(self,parent):
super(problem,self).__init__(parent,-1)
# creates the items
_lbl_l = wx.StaticText(self,-1,"Label")
_tctrl_t = wx.TextCtrl(self,-1)
_btn_b = wx.Button(self,-1,"problem launch")
# button event handling
_btn_b.Bind(wx.EVT_BUTTON,self.OnButton)
# adds the items to the sizer
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.AddMany([ (_lbl_l,0,wx.ALL|wx.ALIGN_LEFT,10),
(_tctrl_t,0,wx.ALL|wx.ALIGN_LEFT,10),
(_btn_b,0,wx.ALL|wx.ALIGN_LEFT,10) ])
# Optimize layout
self.SetSizerAndFit(self.sizer)
self.SetSize(self.sizer.GetSize())
def OnButton(self,evt):
# ------- the problem -------
sizerItem = self.sizer.GetItem(0)
self.sizer.Detach(0)
#self.sizer.AddItem(sizerItem) # it is obvious that AddItem doesn't work
self.sizer.Add(sizerItem.GetWindow(),wx.ALL|wx.ALIGN_LEFT,10)
self.Layout() # you have to call this after adding
# or removing item to sizer to let it size itself
# ---------------------------
app = wx.PySimpleApp()
frame = wx.Frame(None,-1,"frame")
problem(frame)
frame.Show()
app.MainLoop()