]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/PythonEvents.py
6 #----------------------------------------------------------------------
8 # This shows the new 'official' way to do custom events as derived
9 # from the wxPython 2.5 migration guide.
11 #######################################################\
12 # *** Old and busted *** |
14 # myEVT_BUTTON_CLICKPOS = wx.NewEventType() |
16 # def EVT_BUTTON_CLICKPOS(win, id, func): |
17 # win.Connect(id, -1, myEVT_BUTTON_CLICKPOS, func) |
18 #######################################################/
20 #############################\
21 # *** The new Hottness *** |
22 #############################/
23 myEVT_BUTTON_CLICKPOS
= wx
.NewEventType()
24 EVT_BUTTON_CLICKPOS
= wx
.PyEventBinder(myEVT_BUTTON_CLICKPOS
, 1)
26 #----------------------------------------------------------------------
29 class MyEvent(wx
.PyCommandEvent
):
30 def __init__(self
, evtType
, id):
31 wx
.PyCommandEvent
.__init
__(self
, evtType
, id)
36 # wx.PyCommandEvent.__del__(self)
38 def SetMyVal(self
, val
):
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
)
50 def OnLeftDown(self
, event
):
51 pt
= event
.GetPosition()
52 evt
= MyEvent(myEVT_BUTTON_CLICKPOS
, self
.GetId())
54 #print id(evt), sys.getrefcount(evt)
55 self
.GetEventHandler().ProcessEvent(evt
)
56 #print id(evt), sys.getrefcount(evt)
61 class TestPanel(wx
.Panel
):
62 def __init__(self
, parent
, log
):
63 wx
.Panel
.__init
__(self
, parent
, -1)
66 b
= MyButton(self
, -1, " Click me ", (30,30))
67 self
.Bind(wx
.EVT_BUTTON
, self
.OnClick
, id=b
.GetId())
69 # This is our custom event binder created above.
70 self
.Bind(EVT_BUTTON_CLICKPOS
, self
.OnMyEvent
, id=b
.GetId())
73 self
, -1, "Please see the Overview and Demo Code for details...",
78 def OnClick(self
, event
):
79 self
.log
.WriteText("OnClick\n")
81 def OnMyEvent(self
, event
):
82 #print id(event), sys.getrefcount(event)
83 self
.log
.WriteText("MyEvent: %s\n" % (event
.GetMyVal(), ) )
86 #----------------------------------------------------------------------
88 def runTest(frame
, nb
, log
):
89 win
= TestPanel(nb
, log
)
92 #----------------------------------------------------------------------
96 This demo is a contrived example of defining an event class in wxPython and
97 sending it up the containment hierarchy for processing.
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.
104 if __name__
== '__main__':
107 run
.main(['', os
.path
.basename(sys
.argv
[0])])