]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/newevent.py
   2 """Easy generation of new events classes and binder objects""" 
   4 __author__ 
= "Miki Tebeka <tebeka@cs.bgu.ac.il>" 
   8 #--------------------------------------------------------------------------- 
  11     """Generate new (Event, Binder) tuple 
  12         e.g. MooEvent, EVT_MOO = NewEvent() 
  14     evttype 
= wx
.NewEventType() 
  16     class _Event(wx
.PyEvent
): 
  17         def __init__(self
, **kw
): 
  18             wx
.PyEvent
.__init
__(self
) 
  19             self
.SetEventType(evttype
) 
  20             self
.__dict
__.update(kw
) 
  22     return _Event
, wx
.PyEventBinder(evttype
) 
  26 def NewCommandEvent(): 
  27     """Generate new (CmdEvent, Binder) tuple 
  28         e.g. MooCmdEvent, EVT_MOO = NewCommandEvent() 
  30     evttype 
= wx
.NewEventType() 
  32     class _Event(wx
.PyCommandEvent
): 
  33         def __init__(self
, id, **kw
): 
  34             wx
.PyCommandEvent
.__init
__(self
, evttype
, id) 
  35             self
.__dict
__.update(kw
) 
  37     return _Event
, wx
.PyEventBinder(evttype
, 1) 
  40 #--------------------------------------------------------------------------- 
  43     """A little smoke test""" 
  44     from threading 
import Thread
 
  45     from time 
import sleep
 
  47     MooEvent
, EVT_MOO 
= NewEvent() 
  48     GooEvent
, EVT_GOO 
= NewCommandEvent() 
  54         wx
.PostEvent(win
, MooEvent(moo
=1)) 
  58         wx
.PostEvent(win
, GooEvent(id, goo
=id)) 
  63     class Frame(wx
.Frame
): 
  65             wx
.Frame
.__init
__(self
, None, -1, "MOO") 
  66             sizer 
= wx
.BoxSizer(wx
.VERTICAL
) 
  67             EVT_MOO(self
, self
.on_moo
) 
  68             b 
= wx
.Button(self
, -1, "Generate MOO") 
  69             sizer
.Add(b
, 1, wx
.EXPAND
) 
  70             wx
.EVT_BUTTON(self
, b
.GetId(), self
.on_evt_click
) 
  71             b 
= wx
.Button(self
, ID_CMD1
, "Generate GOO with %d" % ID_CMD1
) 
  72             sizer
.Add(b
, 1, wx
.EXPAND
) 
  73             wx
.EVT_BUTTON(self
, ID_CMD1
, self
.on_cmd_click
) 
  74             b 
= wx
.Button(self
, ID_CMD2
, "Generate GOO with %d" % ID_CMD2
) 
  75             sizer
.Add(b
, 1, wx
.EXPAND
) 
  76             wx
.EVT_BUTTON(self
, ID_CMD2
, self
.on_cmd_click
) 
  78             EVT_GOO(self
, ID_CMD1
, self
.on_cmd1
) 
  79             EVT_GOO(self
, ID_CMD2
, self
.on_cmd2
) 
  82             self
.SetAutoLayout(True) 
  85         def on_evt_click(self
, e
): 
  86             t 
= Thread(target
=evt_thr
, args
=(self
, )) 
  90         def on_cmd_click(self
, e
): 
  91             t 
= Thread(target
=cmd_thr
, args
=(self
, e
.GetId())) 
  95         def show(self
, msg
, title
): 
  96             dlg 
= wx
.MessageDialog(self
, msg
, title
, wx
.OK
) 
 101             self
.show("MOO = %s" % e
.moo
, "Got Moo") 
 103         def on_cmd1(self
, e
): 
 104             self
.show("goo = %s" % e
.goo
, "Got Goo (cmd1)") 
 106         def on_cmd2(self
, e
): 
 107             self
.show("goo = %s" % e
.goo
, "Got Goo (cmd2)") 
 110     app 
= wx
.PySimpleApp() 
 115 if __name__ 
== "__main__":