用wxPython建立自銷燬皮膚的方法

jieforest發表於2012-07-01
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:

CODE:

01.import wx
02.
03.########################################################################
04.class PanelOne(wx.Panel):
05.""""""
06.
07.#----------------------------------------------------------------------
08.def __init__(self, parent):
09."""Constructor"""
10.wx.Panel.__init__(self, parent)
11.
12.self.countdown = wx.StaticText(self, label="This panel will self-destruct in 10 seconds")
13.
14.
15.########################################################################
16.class PanelTwo(wx.Panel):
17.""""""
18.
19.#----------------------------------------------------------------------
20.def __init__(self, parent):
21."""Constructor"""
22.wx.Panel.__init__(self, parent)
23.
24.txt = wx.StaticText(self, label="Panel Two")
25.
26.
27.########################################################################
28.class MainFrame(wx.Frame):
29.""""""
30.
31.#----------------------------------------------------------------------
32.def __init__(self):
33."""Constructor"""
34.wx.Frame.__init__(self, None, title="Panel Smacker")
35.self.panelOne = PanelOne(self)
36.self.time2die = 10
37.
38.self.timer = wx.Timer(self)
39.self.Bind(wx.EVT_TIMER, self.update, self.timer)
40.self.timer.Start(1000)
41.
42.self.sizer = wx.BoxSizer(wx.VERTICAL)
43.self.sizer.Add(self.panelOne, 1, wx.EXPAND)
44.self.SetSizer(self.sizer)
45.
46.#----------------------------------------------------------------------
47.def update(self, event):
48.""""""
49.if self.time2die < 0:
50.self.panelOne.Destroy()
51.self.panelTwo = PanelTwo(self)
52.self.sizer.Add(self.panelTwo, 1, wx.EXPAND)
53.self.Layout()
54.self.timer.Stop()
55.else:
56.msg = "This panel will self-destruct in %s seconds" %self.time2die
57.self.panelOne.countdown.SetLabel(msg)
58.self.time2die -= 1
59.
60.if __name__ == "__main__":
61.app = wx.App(False)
62.frame. = MainFrame()
63.frame.Show()
64.app.MainLoop()

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-734244/,如需轉載,請註明出處,否則將追究法律責任。

相關文章