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