]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/PythonEvents.py
reSWIGged
[wxWidgets.git] / wxPython / demo / PythonEvents.py
CommitLineData
8fa876ca
RD
1# 11/13/2003 - Jeff Grimmett (grimmtooth@softhome.net)
2#
3# o Updated for wx namespace
4#
65dd82cb 5
8fa876ca
RD
6import sys
7
8import wx
65dd82cb
RD
9
10#----------------------------------------------------------------------
11
8fa876ca
RD
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#######################################################/
65dd82cb 23
8fa876ca
RD
24#############################\
25# *** The new Hottness *** |
26#############################/
27myEVT_BUTTON_CLICKPOS = wx.NewEventType()
28EVT_BUTTON_CLICKPOS = wx.PyEventBinder(myEVT_BUTTON_CLICKPOS, 1)
65dd82cb 29
8fa876ca 30#----------------------------------------------------------------------
65dd82cb
RD
31
32
8fa876ca 33class MyEvent(wx.PyCommandEvent):
65dd82cb 34 def __init__(self, evtType, id):
8fa876ca 35 wx.PyCommandEvent.__init__(self, evtType, id)
65dd82cb
RD
36 self.myVal = None
37
38 #def __del__(self):
39 # print '__del__'
8fa876ca 40 # wx.PyCommandEvent.__del__(self)
65dd82cb
RD
41
42 def SetMyVal(self, val):
43 self.myVal = val
44
45 def GetMyVal(self):
46 return self.myVal
47
48
8fa876ca 49class MyButton(wx.Button):
65dd82cb 50 def __init__(self, parent, id, txt, pos):
8fa876ca
RD
51 wx.Button.__init__(self, parent, id, txt, pos)
52 self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
65dd82cb
RD
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
8fa876ca 65class TestPanel(wx.Panel):
65dd82cb 66 def __init__(self, parent, log):
8fa876ca 67 wx.Panel.__init__(self, parent, -1)
65dd82cb
RD
68 self.log = log
69
8fa876ca
RD
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())
65dd82cb 75
8fa876ca
RD
76 wx.StaticText(
77 self, -1, "Please see the Overview and Demo Code for details...",
78 (30, 80)
79 )
65dd82cb
RD
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
92def runTest(frame, nb, log):
93 win = TestPanel(nb, log)
94 return win
95
96#----------------------------------------------------------------------
97
98
65dd82cb 99overview = """\
8fa876ca
RD
100This demo is a contrived example of defining an event class in wxPython and
101sending it up the containment hierarchy for processing.
65dd82cb 102
8fa876ca
RD
103V2.5 note: this demo also shows the new style of creating event binders that
104is required if you used the *.Bind() method of setting up event handlers.
105"""
1fded56b
RD
106
107
108if __name__ == '__main__':
109 import sys,os
110 import run
111 run.main(['', os.path.basename(sys.argv[0])])
112