The other day I saw a question on StackOverflow about how to dynamically destroy and create panels after a certain amount of time has passed. I told the fellow that he could use the examples from one of my blog articles where I destroyed and created buttons, but the dude just didn’t get it. So I wrote a simple example where the panel displays a count down and then destroys itself and is promptly replaced with another panel.

Here’s the code for your viewing pleasure:

import wx ######################################################################## class PanelOne(wx.Panel): """""" #---------------------------------------------------------------------- def __init__(self, parent): """Constructor""" wx.Panel.__init__(self, parent) self.countdown = wx.StaticText(self, label="This panel will self-destruct in 10 seconds") ######################################################################## class PanelTwo(wx.Panel): """""" #---------------------------------------------------------------------- def __init__(self, parent): """Constructor""" wx.Panel.__init__(self, parent) txt = wx.StaticText(self, label="Panel Two") ######################################################################## class MainFrame(wx.Frame): """""" #---------------------------------------------------------------------- def __init__(self): """Constructor""" wx.Frame.__init__(self, None, title="Panel Smacker") self.panelOne = PanelOne(self) self.time2die = 10 self.timer = wx.Timer(self) self.Bind(wx.EVT_TIMER, self.update, self.timer) self.timer.Start(1000) self.sizer = wx.BoxSizer(wx.VERTICAL) self.sizer.Add(self.panelOne, 1, wx.EXPAND) self.SetSizer(self.sizer) #---------------------------------------------------------------------- def update(self, event): """""" if self.time2die < 0: self.panelOne.Destroy() self.panelTwo = PanelTwo(self) self.sizer.Add(self.panelTwo, 1, wx.EXPAND) self.Layout() self.timer.Stop() else: msg = "This panel will self-destruct in %s seconds" % self.time2die self.panelOne.countdown.SetLabel(msg) self.time2die -= 1 if __name__ == "__main__": app = wx.App(False) frame = MainFrame() frame.Show() app.MainLoop()

When you run this code, you should see something like this:

It will countdown for 10 seconds and then you'll see this:

If you want to learn more about timers, I wrote an article on that as well. Enjoy!