]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/PythonEvents.py
1 # 11/13/2003 - Jeff Grimmett (grimmtooth@softhome.net)
3 # o Updated for wx namespace
10 #----------------------------------------------------------------------
12 # This shows the new 'official' way to do custom events as derived
13 # from the wxPython 2.5 migration guide.
15 #######################################################\
16 # *** Old and busted *** |
18 # myEVT_BUTTON_CLICKPOS = wx.NewEventType() |
20 # def EVT_BUTTON_CLICKPOS(win, id, func): |
21 # win.Connect(id, -1, myEVT_BUTTON_CLICKPOS, func) |
22 #######################################################/
24 #############################\
25 # *** The new Hottness *** |
26 #############################/
27 myEVT_BUTTON_CLICKPOS
= wx
.NewEventType()
28 EVT_BUTTON_CLICKPOS
= wx
.PyEventBinder(myEVT_BUTTON_CLICKPOS
, 1)
30 #----------------------------------------------------------------------
33 class MyEvent(wx
.PyCommandEvent
):
34 def __init__(self
, evtType
, id):
35 wx
.PyCommandEvent
.__init
__(self
, evtType
, id)
40 # wx.PyCommandEvent.__del__(self)
42 def SetMyVal(self
, val
):
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
)
54 def OnLeftDown(self
, event
):
55 pt
= event
.GetPosition()
56 evt
= MyEvent(myEVT_BUTTON_CLICKPOS
, self
.GetId())
58 #print id(evt), sys.getrefcount(evt)
59 self
.GetEventHandler().ProcessEvent(evt
)
60 #print id(evt), sys.getrefcount(evt)
65 class TestPanel(wx
.Panel
):
66 def __init__(self
, parent
, log
):
67 wx
.Panel
.__init
__(self
, parent
, -1)
70 b
= MyButton(self
, -1, " Click me ", (30,30))
71 self
.Bind(wx
.EVT_BUTTON
, self
.OnClick
, id=b
.GetId())
73 # This is our custom event binder created above.
74 self
.Bind(EVT_BUTTON_CLICKPOS
, self
.OnMyEvent
, id=b
.GetId())
77 self
, -1, "Please see the Overview and Demo Code for details...",
82 def OnClick(self
, event
):
83 self
.log
.WriteText("OnClick\n")
85 def OnMyEvent(self
, event
):
86 #print id(event), sys.getrefcount(event)
87 self
.log
.WriteText("MyEvent: %s\n" % (event
.GetMyVal(), ) )
90 #----------------------------------------------------------------------
92 def runTest(frame
, nb
, log
):
93 win
= TestPanel(nb
, log
)
96 #----------------------------------------------------------------------
100 This demo is a contrived example of defining an event class in wxPython and
101 sending it up the containment hierarchy for processing.
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.
108 if __name__
== '__main__':
111 run
.main(['', os
.path
.basename(sys
.argv
[0])])