用wxPython创建自销毁面板的方法

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/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/301743/viewspace-734244/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值