]> git.saurik.com Git - wxWidgets.git/blame - wxPython/wx/lib/newevent.py
Fixed the double traceback when an exception happens in OnInit
[wxWidgets.git] / wxPython / wx / lib / newevent.py
CommitLineData
d14a1e28
RD
1#!/usr/bin/env python
2"""Easy generation of new events classes and binder functions"""
8b9a4190 3
d14a1e28 4__author__ = "Miki Tebeka <tebeka@cs.bgu.ac.il>"
8b9a4190 5
d14a1e28
RD
6import wx
7
8#---------------------------------------------------------------------------
9
10def 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 def Binder(win, func):
23 win.Connect(-1, -1, evttype, func)
24
25 return _Event, Binder
26
27
28
29def NewCommandEvent():
30 """Generate new (CmdEvent, Binder) tuple
31 e.g. MooCmdEvent, EVT_MOO = NewCommandEvent()
32 """
33 evttype = wx.NewEventType()
34
35 class _Event(wx.PyCommandEvent):
36 def __init__(self, id, **kw):
37 wx.PyCommandEvent.__init__(self, evttype, id)
38 self.__dict__.update(kw)
39
40 def Binder(win, id, func):
41 win.Connect(id, -1, evttype, func)
42
43 return _Event, Binder
44
45
46#---------------------------------------------------------------------------
47
48def _test():
49 """A little smoke test"""
50 from threading import Thread
51 from time import sleep
52
53 MooEvent, EVT_MOO = NewEvent()
54 GooEvent, EVT_GOO = NewCommandEvent()
55
56 DELAY = 0.7
57
58 def evt_thr(win):
59 sleep(DELAY)
60 wx.PostEvent(win, MooEvent(moo=1))
61
62 def cmd_thr(win, id):
63 sleep(DELAY)
64 wx.PostEvent(win, GooEvent(id, goo=id))
65
66 ID_CMD1 = wx.NewId()
67 ID_CMD2 = wx.NewId()
68
69 class Frame(wx.Frame):
70 def __init__(self):
71 wx.Frame.__init__(self, None, -1, "MOO")
72 sizer = wx.BoxSizer(wx.VERTICAL)
73 EVT_MOO(self, self.on_moo)
74 b = wx.Button(self, -1, "Generate MOO")
75 sizer.Add(b, 1, wx.EXPAND)
76 wx.EVT_BUTTON(self, b.GetId(), self.on_evt_click)
77 b = wx.Button(self, ID_CMD1, "Generate GOO with %d" % ID_CMD1)
78 sizer.Add(b, 1, wx.EXPAND)
79 wx.EVT_BUTTON(self, ID_CMD1, self.on_cmd_click)
80 b = wx.Button(self, ID_CMD2, "Generate GOO with %d" % ID_CMD2)
81 sizer.Add(b, 1, wx.EXPAND)
82 wx.EVT_BUTTON(self, ID_CMD2, self.on_cmd_click)
83
84 EVT_GOO(self, ID_CMD1, self.on_cmd1)
85 EVT_GOO(self, ID_CMD2, self.on_cmd2)
86
87 self.SetSizer(sizer)
88 self.SetAutoLayout(True)
89 sizer.Fit(self)
90
91 def on_evt_click(self, e):
92 t = Thread(target=evt_thr, args=(self, ))
93 t.setDaemon(True)
94 t.start()
95
96 def on_cmd_click(self, e):
97 t = Thread(target=cmd_thr, args=(self, e.GetId()))
98 t.setDaemon(True)
99 t.start()
100
101 def show(self, msg, title):
102 dlg = wx.MessageDialog(self, msg, title, wx.OK)
103 dlg.ShowModal()
104 dlg.Destroy()
105
106 def on_moo(self, e):
107 self.show("MOO = %s" % e.moo, "Got Moo")
108
109 def on_cmd1(self, e):
110 self.show("goo = %s" % e.goo, "Got Goo (cmd1)")
111
112 def on_cmd2(self, e):
113 self.show("goo = %s" % e.goo, "Got Goo (cmd2)")
114
115
116 app = wx.PySimpleApp()
117 f = Frame()
118 f.Show(True)
119 app.MainLoop()
120
121if __name__ == "__main__":
122 _test()