After further testing …
The code below is working, as far as it goes, but the OnDelete code is
still not getting rid of everything. I know that because as soon as
I add a new panel (with additional code of course), the supposedly
deleted panels come back!
What am I still missing here?
Bob
···
Date: Sun, 29 Oct 2006 00:43:45
-0400To: wxPython users
From: Bob Klahn bobstones@comcast.net
Subject: Fwd: Re: [wxPython-users] Display update after detaching the
last panel from a flex grid sizer – Destroy?At 09:58 PM 10/28/2006, I > > wrote:
Bob Klahn wrote:
What do I need to do to the
sample code below to cause the display to update after deleting (vial
Panel | Detach leading panel) the last panel?Detach doesn’t destroy the window, so it is still where it was when the
sizer stopped managing it. If you want it to be totally gone you
need to Destroy() yourself.Hmmm, then why do all but the last panel disappear from the display when
I detach them?? I haven’t destroyed any of them.Hmmm, did garbage collection delete all but the last window? If so,
then what was my program holding onto that prevented the last detach
operation from (indirectly) deleting the window?Here’s my revised demo code, which seems to work, followed by an
additional question or two:
`import wx
class MainFrame(wx.Frame):
def init(self, parent, id, title):
wx.Frame.__init__(self, parent, -1,
title)
self.menu = wx.Menu() item_DELETE = self.menu.Append(101,
“&Delete leading panel”)
menuBar = wx.MenuBar() menuBar.Append(self.menu,
“&Panel”,)
self.SetMenuBar(menuBar) self.Bind(wx.EVT_MENU,
self.OnDelete, item_DELETE)
pnl = wx.Panel(self,-1) self.mainsizer =
wx.BoxSizer(wx.VERTICAL)
self.fgs = wx.FlexGridSizer(1, 3, 1,
rows, cols, vgap, hgap
self.fgs.AddGrowableRow(0)
self.fgs.AddGrowableCol(0)
self.fgs.AddGrowableCol(1)
self.fgs.AddGrowableCol(2)
self.fgs.Add(ChildPanel(pnl,
-1,‘win00’), flag=wx.EXPAND)self.fgs.Add(ChildPanel(pnl,
-1,‘win01’), flag=wx.EXPAND)self.fgs.Add(ChildPanel(pnl,
-1,‘win02’), flag=wx.EXPAND)pnl.SetSizer(self.fgs)
self.mainsizer.Add(pnl, 1,
wx.EXPAND)self.SetSizer(self.mainsizer)
self.fgs.Layout() self.mainsizer.Layout()
def OnDelete(self,evt):
"""Detach and
destroy flex grid sizer’s leading panel"“”
sizer_items =
self.fgs.GetChildren()
win =
sizer_items[0].GetWindow()
self.fgs.Detach(0)
win.Destroy() self.fgs.Layout()
self.mainsizer.Layout()
class ChildPanel(wx.Panel):
def init(self, parent, id, title):
wx.Panel.__init__(self, parent, -1,
style=wx.RAISED_BORDER)
pnl = wx.Panel(self,-1)
pnl.SetBackgroundColour(wx.BLUE)
st_ttl = wx.StaticText(self, -1,
title, size=(20,20)
,
style=wx.ALIGN_CENTER|wx.RAISED_BORDER|wx.ST_NO_AUTORESIZE)
st_ttl.SetBackgroundColour(wx.WHITE)
bs = wx.BoxSizer(wx.VERTICAL) bs.Add(st_ttl, 0,
wx.EXPAND)
bs.Add(pnl, 1, wx.EXPAND) self.SetSizer(bs) bs.Layout()
if name == ‘main’:
app = wx.App(redirect=False)
frame = MainFrame(None, -1, “Titled-panels-in-frame
detach test”)frame.Show()
app.MainLoop()
`
(1) Have I coded my OnDelete method the way you would code it?
(2) Since I’m now destroying each child panel, the sizer detach, which
I’ve commented out, appears to be unnecessary. Or would it be good
form to code it anyway?(3) A more fundamental, OO-novice question: I seemed to need all
those “self”'s above, but I gotta believe there’s a cleaner,
tighter way. ??Bob