]>
Commit | Line | Data |
---|---|---|
65dd82cb | 1 | |
8fa876ca RD |
2 | import sys |
3 | ||
4 | import wx | |
65dd82cb RD |
5 | |
6 | #---------------------------------------------------------------------- | |
7 | ||
8fa876ca RD |
8 | # This shows the new 'official' way to do custom events as derived |
9 | # from the wxPython 2.5 migration guide. | |
10 | ||
11 | #######################################################\ | |
12 | # *** Old and busted *** | | |
13 | # | | |
14 | # myEVT_BUTTON_CLICKPOS = wx.NewEventType() | | |
15 | # | | |
16 | # def EVT_BUTTON_CLICKPOS(win, id, func): | | |
17 | # win.Connect(id, -1, myEVT_BUTTON_CLICKPOS, func) | | |
18 | #######################################################/ | |
65dd82cb | 19 | |
8fa876ca RD |
20 | #############################\ |
21 | # *** The new Hottness *** | | |
22 | #############################/ | |
23 | myEVT_BUTTON_CLICKPOS = wx.NewEventType() | |
24 | EVT_BUTTON_CLICKPOS = wx.PyEventBinder(myEVT_BUTTON_CLICKPOS, 1) | |
65dd82cb | 25 | |
8fa876ca | 26 | #---------------------------------------------------------------------- |
65dd82cb RD |
27 | |
28 | ||
8fa876ca | 29 | class MyEvent(wx.PyCommandEvent): |
65dd82cb | 30 | def __init__(self, evtType, id): |
8fa876ca | 31 | wx.PyCommandEvent.__init__(self, evtType, id) |
65dd82cb RD |
32 | self.myVal = None |
33 | ||
34 | #def __del__(self): | |
35 | # print '__del__' | |
8fa876ca | 36 | # wx.PyCommandEvent.__del__(self) |
65dd82cb RD |
37 | |
38 | def SetMyVal(self, val): | |
39 | self.myVal = val | |
40 | ||
41 | def GetMyVal(self): | |
42 | return self.myVal | |
43 | ||
44 | ||
8fa876ca | 45 | class MyButton(wx.Button): |
65dd82cb | 46 | def __init__(self, parent, id, txt, pos): |
8fa876ca RD |
47 | wx.Button.__init__(self, parent, id, txt, pos) |
48 | self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown) | |
65dd82cb RD |
49 | |
50 | def OnLeftDown(self, event): | |
51 | pt = event.GetPosition() | |
52 | evt = MyEvent(myEVT_BUTTON_CLICKPOS, self.GetId()) | |
53 | evt.SetMyVal(pt) | |
54 | #print id(evt), sys.getrefcount(evt) | |
55 | self.GetEventHandler().ProcessEvent(evt) | |
56 | #print id(evt), sys.getrefcount(evt) | |
57 | event.Skip() | |
58 | ||
59 | ||
60 | ||
8fa876ca | 61 | class TestPanel(wx.Panel): |
65dd82cb | 62 | def __init__(self, parent, log): |
8fa876ca | 63 | wx.Panel.__init__(self, parent, -1) |
65dd82cb RD |
64 | self.log = log |
65 | ||
8fa876ca RD |
66 | b = MyButton(self, -1, " Click me ", (30,30)) |
67 | self.Bind(wx.EVT_BUTTON, self.OnClick, id=b.GetId()) | |
68 | ||
69 | # This is our custom event binder created above. | |
70 | self.Bind(EVT_BUTTON_CLICKPOS, self.OnMyEvent, id=b.GetId()) | |
65dd82cb | 71 | |
8fa876ca RD |
72 | wx.StaticText( |
73 | self, -1, "Please see the Overview and Demo Code for details...", | |
74 | (30, 80) | |
75 | ) | |
65dd82cb RD |
76 | |
77 | ||
78 | def OnClick(self, event): | |
79 | self.log.WriteText("OnClick\n") | |
80 | ||
81 | def OnMyEvent(self, event): | |
82 | #print id(event), sys.getrefcount(event) | |
83 | self.log.WriteText("MyEvent: %s\n" % (event.GetMyVal(), ) ) | |
84 | ||
85 | ||
86 | #---------------------------------------------------------------------- | |
87 | ||
88 | def runTest(frame, nb, log): | |
89 | win = TestPanel(nb, log) | |
90 | return win | |
91 | ||
92 | #---------------------------------------------------------------------- | |
93 | ||
94 | ||
65dd82cb | 95 | overview = """\ |
8fa876ca RD |
96 | This demo is a contrived example of defining an event class in wxPython and |
97 | sending it up the containment hierarchy for processing. | |
65dd82cb | 98 | |
8fa876ca RD |
99 | V2.5 note: this demo also shows the new style of creating event binders that |
100 | is required if you used the *.Bind() method of setting up event handlers. | |
101 | """ | |
1fded56b RD |
102 | |
103 | ||
104 | if __name__ == '__main__': | |
105 | import sys,os | |
106 | import run | |
8eca4fef | 107 | run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:]) |
1fded56b | 108 |