]>
Commit | Line | Data |
---|---|---|
1 | ||
2 | import sys | |
3 | ||
4 | import wx | |
5 | ||
6 | #---------------------------------------------------------------------- | |
7 | ||
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 | #######################################################/ | |
19 | ||
20 | #############################\ | |
21 | # *** The new Hottness *** | | |
22 | #############################/ | |
23 | myEVT_BUTTON_CLICKPOS = wx.NewEventType() | |
24 | EVT_BUTTON_CLICKPOS = wx.PyEventBinder(myEVT_BUTTON_CLICKPOS, 1) | |
25 | ||
26 | #---------------------------------------------------------------------- | |
27 | ||
28 | ||
29 | class MyEvent(wx.PyCommandEvent): | |
30 | def __init__(self, evtType, id): | |
31 | wx.PyCommandEvent.__init__(self, evtType, id) | |
32 | self.myVal = None | |
33 | ||
34 | #def __del__(self): | |
35 | # print '__del__' | |
36 | # wx.PyCommandEvent.__del__(self) | |
37 | ||
38 | def SetMyVal(self, val): | |
39 | self.myVal = val | |
40 | ||
41 | def GetMyVal(self): | |
42 | return self.myVal | |
43 | ||
44 | ||
45 | class MyButton(wx.Button): | |
46 | def __init__(self, parent, id, txt, pos): | |
47 | wx.Button.__init__(self, parent, id, txt, pos) | |
48 | self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown) | |
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 | ||
61 | class TestPanel(wx.Panel): | |
62 | def __init__(self, parent, log): | |
63 | wx.Panel.__init__(self, parent, -1) | |
64 | self.log = log | |
65 | ||
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()) | |
71 | ||
72 | wx.StaticText( | |
73 | self, -1, "Please see the Overview and Demo Code for details...", | |
74 | (30, 80) | |
75 | ) | |
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 | ||
95 | overview = """\ | |
96 | This demo is a contrived example of defining an event class in wxPython and | |
97 | sending it up the containment hierarchy for processing. | |
98 | ||
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 | """ | |
102 | ||
103 | ||
104 | if __name__ == '__main__': | |
105 | import sys,os | |
106 | import run | |
107 | run.main(['', os.path.basename(sys.argv[0])]) | |
108 |