使用定时器切换Panel显示

#!/usr/bin/python
#-*-coding:utf-8-*-


import wx
import time

class PanelOne(wx.Panel):
	def __init__(self, parent):
		wx.Panel.__init__(self, parent)
		self.countdown = wx.StaticText(self,label="This panel will self-destruct in 2 seconds")

class PanelTwo(wx.Panel):
	def __init__(self, parent):
		wx.Panel.__init__(self, parent)
		txt = wx.StaticText(self,label="Panel Two")


class MainFrame(wx.Frame):
	def __init__(self, title):
		"""Constructor"""
		wx.Frame.__init__(self, None, title=title, size=(400, 400))
		self.panelOne = PanelOne(self)
		self.time2die = 2

		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:
			print "time is up!"
			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()
	win = MainFrame("xxx")
	#win = wx.Frame(None, title="python git", size=(410, 335))
	win.Show()
	app.MainLoop()

创建/销毁Panel

用class的方式建立Panel

定时器 timer

你可能感兴趣的:(使用定时器切换Panel显示)