爱是永不止息 2012-07-03
前些日子我看见StackOverflow的一个问题,说如何动态地延时创建以及销毁容器。我那时就回答说他可以参考我以前写过的有关于如何动态创建与销毁按钮的技术博客,但也许对方实在太懒而根本没有考虑那个建议。
所以这篇文章中,我演示给大家一个范例,看看如何动态延时创建一个容器并接着销毁的过程。
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()
然后大概10秒后,你才会看到如下图这样: