Advertisement
DevPlayer

Evthandler needs a widget?

Oct 2nd, 2011
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1.  
  2.  
  3. # DevPlayer @ gmai ..lcom
  4. # 2011-Oct-2
  5.  
  6. # EventHandler
  7.  
  8.  
  9.  
  10. import sys
  11. import wx
  12. import time
  13.  
  14.  
  15. class MyTimer(wx.Timer):
  16.  
  17.     MS_DEFAULT = 1000
  18.     countTo = 1
  19.     counted = 0
  20.  
  21.     def __init__(self, owner=None, Id=-1):
  22.         print('MyTimer() initializing')
  23.         me = self
  24.         wx.Timer.__init__(self, owner, Id)
  25.  
  26.         self.counted = 0
  27.         self.countTo = self.MS_DEFAULT
  28.         self.interval = self.MS_DEFAULT
  29.  
  30.         # comment next line to see MainLoop() just drop out.
  31.         self.frame = wx.Frame(None) # doesn't work without it
  32.  
  33.         self.Bind(wx.EVT_TIMER, self.OnTimer)
  34.  
  35.         #self.frame.Bind(wx.EVT_CLOSE, self.OnClose)
  36.         self.Bind(wx.EVT_CLOSE, self.OnClose)
  37.  
  38.         print('MyTimer() Initialized')
  39.  
  40.  
  41.     def Start(self, ms_length=MS_DEFAULT,
  42.             ms_interval=MS_DEFAULT, oneShot=False):
  43.  
  44.         print('MyTimer().Start(%d, %d, %s)' % (ms_length, ms_interval, oneShot))
  45.         self.countTo = ms_length
  46.  
  47.         val = super(MyTimer, self).Start(
  48.             milliseconds=ms_interval, oneShot=oneShot)
  49.  
  50.         return val
  51.  
  52.  
  53.     def OnTimer(self, event):
  54.  
  55.         if self.counted >= self.countTo:
  56.             print('Times up !')
  57.             if  hasattr(self, 'frame')                          \
  58.                 and hasattr(self.frame, 'Close')                \
  59.                 and isinstance( self.frame.Close, type(self.OnTimer)):
  60.  
  61.                 self.frame.Close()
  62.                 #self.frame.Destroy()
  63.             else:
  64.                 self.Destroy()
  65.  
  66.         text = time.strftime('%I:%M:%S %p', time.localtime())
  67.         print('%07d: %s' % (self.counted, text))
  68.  
  69.         self.counted += self.interval
  70.         event.Skip()
  71.  
  72.  
  73.     def OnClose(self, event):
  74.         print('Closing')
  75.         self.Stop()
  76.         event.Skip()
  77.  
  78.  
  79.  
  80. print('Starting')
  81. app = wx.App(0)
  82. timer = MyTimer()
  83. timer.Start(10000, 1000, False)
  84. app.MainLoop()
  85. print('Finished')
  86.  
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement