]>
Commit | Line | Data |
---|---|---|
3252e504 | 1 | """Easy generation of new events classes and binder objects""" |
8b9a4190 | 2 | |
d14a1e28 | 3 | __author__ = "Miki Tebeka <tebeka@cs.bgu.ac.il>" |
8b9a4190 | 4 | |
d14a1e28 RD |
5 | import wx |
6 | ||
7 | #--------------------------------------------------------------------------- | |
8 | ||
9 | def NewEvent(): | |
10 | """Generate new (Event, Binder) tuple | |
11 | e.g. MooEvent, EVT_MOO = NewEvent() | |
12 | """ | |
13 | evttype = wx.NewEventType() | |
14 | ||
15 | class _Event(wx.PyEvent): | |
16 | def __init__(self, **kw): | |
17 | wx.PyEvent.__init__(self) | |
18 | self.SetEventType(evttype) | |
19 | self.__dict__.update(kw) | |
20 | ||
3252e504 | 21 | return _Event, wx.PyEventBinder(evttype) |
d14a1e28 RD |
22 | |
23 | ||
24 | ||
25 | def NewCommandEvent(): | |
26 | """Generate new (CmdEvent, Binder) tuple | |
27 | e.g. MooCmdEvent, EVT_MOO = NewCommandEvent() | |
28 | """ | |
29 | evttype = wx.NewEventType() | |
30 | ||
31 | class _Event(wx.PyCommandEvent): | |
32 | def __init__(self, id, **kw): | |
33 | wx.PyCommandEvent.__init__(self, evttype, id) | |
34 | self.__dict__.update(kw) | |
35 | ||
3252e504 | 36 | return _Event, wx.PyEventBinder(evttype, 1) |
d14a1e28 RD |
37 | |
38 | ||
39 | #--------------------------------------------------------------------------- | |
40 | ||
41 | def _test(): | |
42 | """A little smoke test""" | |
43 | from threading import Thread | |
44 | from time import sleep | |
45 | ||
46 | MooEvent, EVT_MOO = NewEvent() | |
47 | GooEvent, EVT_GOO = NewCommandEvent() | |
48 | ||
49 | DELAY = 0.7 | |
50 | ||
51 | def evt_thr(win): | |
52 | sleep(DELAY) | |
53 | wx.PostEvent(win, MooEvent(moo=1)) | |
54 | ||
55 | def cmd_thr(win, id): | |
56 | sleep(DELAY) | |
57 | wx.PostEvent(win, GooEvent(id, goo=id)) | |
58 | ||
59 | ID_CMD1 = wx.NewId() | |
60 | ID_CMD2 = wx.NewId() | |
61 | ||
62 | class Frame(wx.Frame): | |
63 | def __init__(self): | |
64 | wx.Frame.__init__(self, None, -1, "MOO") | |
65 | sizer = wx.BoxSizer(wx.VERTICAL) | |
66 | EVT_MOO(self, self.on_moo) | |
67 | b = wx.Button(self, -1, "Generate MOO") | |
68 | sizer.Add(b, 1, wx.EXPAND) | |
69 | wx.EVT_BUTTON(self, b.GetId(), self.on_evt_click) | |
70 | b = wx.Button(self, ID_CMD1, "Generate GOO with %d" % ID_CMD1) | |
71 | sizer.Add(b, 1, wx.EXPAND) | |
72 | wx.EVT_BUTTON(self, ID_CMD1, self.on_cmd_click) | |
73 | b = wx.Button(self, ID_CMD2, "Generate GOO with %d" % ID_CMD2) | |
74 | sizer.Add(b, 1, wx.EXPAND) | |
75 | wx.EVT_BUTTON(self, ID_CMD2, self.on_cmd_click) | |
76 | ||
77 | EVT_GOO(self, ID_CMD1, self.on_cmd1) | |
78 | EVT_GOO(self, ID_CMD2, self.on_cmd2) | |
79 | ||
80 | self.SetSizer(sizer) | |
81 | self.SetAutoLayout(True) | |
82 | sizer.Fit(self) | |
83 | ||
84 | def on_evt_click(self, e): | |
85 | t = Thread(target=evt_thr, args=(self, )) | |
86 | t.setDaemon(True) | |
87 | t.start() | |
88 | ||
89 | def on_cmd_click(self, e): | |
90 | t = Thread(target=cmd_thr, args=(self, e.GetId())) | |
91 | t.setDaemon(True) | |
92 | t.start() | |
93 | ||
94 | def show(self, msg, title): | |
95 | dlg = wx.MessageDialog(self, msg, title, wx.OK) | |
96 | dlg.ShowModal() | |
97 | dlg.Destroy() | |
98 | ||
99 | def on_moo(self, e): | |
100 | self.show("MOO = %s" % e.moo, "Got Moo") | |
101 | ||
102 | def on_cmd1(self, e): | |
103 | self.show("goo = %s" % e.goo, "Got Goo (cmd1)") | |
104 | ||
105 | def on_cmd2(self, e): | |
106 | self.show("goo = %s" % e.goo, "Got Goo (cmd2)") | |
107 | ||
108 | ||
109 | app = wx.PySimpleApp() | |
110 | f = Frame() | |
111 | f.Show(True) | |
112 | app.MainLoop() | |
113 | ||
114 | if __name__ == "__main__": | |
115 | _test() |