]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/newevent.py
Fixed the docstring, default module is now wx, not wxPython.wx
[wxWidgets.git] / wxPython / wx / lib / newevent.py
1 #!/usr/bin/env python
2 """Easy generation of new events classes and binder objects"""
3
4 __author__ = "Miki Tebeka <tebeka@cs.bgu.ac.il>"
5
6 import wx
7
8 #---------------------------------------------------------------------------
9
10 def NewEvent():
11 """Generate new (Event, Binder) tuple
12 e.g. MooEvent, EVT_MOO = NewEvent()
13 """
14 evttype = wx.NewEventType()
15
16 class _Event(wx.PyEvent):
17 def __init__(self, **kw):
18 wx.PyEvent.__init__(self)
19 self.SetEventType(evttype)
20 self.__dict__.update(kw)
21
22 return _Event, wx.PyEventBinder(evttype)
23
24
25
26 def NewCommandEvent():
27 """Generate new (CmdEvent, Binder) tuple
28 e.g. MooCmdEvent, EVT_MOO = NewCommandEvent()
29 """
30 evttype = wx.NewEventType()
31
32 class _Event(wx.PyCommandEvent):
33 def __init__(self, id, **kw):
34 wx.PyCommandEvent.__init__(self, evttype, id)
35 self.__dict__.update(kw)
36
37 return _Event, wx.PyEventBinder(evttype, 1)
38
39
40 #---------------------------------------------------------------------------
41
42 def _test():
43 """A little smoke test"""
44 from threading import Thread
45 from time import sleep
46
47 MooEvent, EVT_MOO = NewEvent()
48 GooEvent, EVT_GOO = NewCommandEvent()
49
50 DELAY = 0.7
51
52 def evt_thr(win):
53 sleep(DELAY)
54 wx.PostEvent(win, MooEvent(moo=1))
55
56 def cmd_thr(win, id):
57 sleep(DELAY)
58 wx.PostEvent(win, GooEvent(id, goo=id))
59
60 ID_CMD1 = wx.NewId()
61 ID_CMD2 = wx.NewId()
62
63 class Frame(wx.Frame):
64 def __init__(self):
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)
77
78 EVT_GOO(self, ID_CMD1, self.on_cmd1)
79 EVT_GOO(self, ID_CMD2, self.on_cmd2)
80
81 self.SetSizer(sizer)
82 self.SetAutoLayout(True)
83 sizer.Fit(self)
84
85 def on_evt_click(self, e):
86 t = Thread(target=evt_thr, args=(self, ))
87 t.setDaemon(True)
88 t.start()
89
90 def on_cmd_click(self, e):
91 t = Thread(target=cmd_thr, args=(self, e.GetId()))
92 t.setDaemon(True)
93 t.start()
94
95 def show(self, msg, title):
96 dlg = wx.MessageDialog(self, msg, title, wx.OK)
97 dlg.ShowModal()
98 dlg.Destroy()
99
100 def on_moo(self, e):
101 self.show("MOO = %s" % e.moo, "Got Moo")
102
103 def on_cmd1(self, e):
104 self.show("goo = %s" % e.goo, "Got Goo (cmd1)")
105
106 def on_cmd2(self, e):
107 self.show("goo = %s" % e.goo, "Got Goo (cmd2)")
108
109
110 app = wx.PySimpleApp()
111 f = Frame()
112 f.Show(True)
113 app.MainLoop()
114
115 if __name__ == "__main__":
116 _test()