]> git.saurik.com Git - wxWidgets.git/blame - wxPython/samples/wxPIA_book/Chapter-03/customEvent.py
Adding overview docs and a couple tweaks to the demo.
[wxWidgets.git] / wxPython / samples / wxPIA_book / Chapter-03 / customEvent.py
CommitLineData
be05b434
RD
1import wx
2
3class TwoButtonEvent(wx.PyCommandEvent):
4 def __init__(self, evtType, id):
5 wx.PyCommandEvent.__init__(self, evtType, id)
6 self.clickCount = 0
7
8 def GetClickCount(self):
9 return self.clickCount
10
11 def SetClickCount(self, count):
12 self.clickCount = count
13
14myEVT_TWO_BUTTON = wx.NewEventType()
15EVT_TWO_BUTTON = wx.PyEventBinder(myEVT_TWO_BUTTON, 1)
16
17class TwoButtonPanel(wx.Panel):
18 def __init__(self, parent, id=-1, leftText="Left",
19 rightText="Right"):
20 wx.Panel.__init__(self, parent, id)
21 self.leftButton = wx.Button(self, label=leftText)
22 self.rightButton = wx.Button(self, label=rightText,
23 pos=(100,0))
24 self.leftClick = False
25 self.rightClick = False
26 self.clickCount = 0
27 self.leftButton.Bind(wx.EVT_LEFT_DOWN, self.OnLeftClick)
28 self.rightButton.Bind(wx.EVT_LEFT_DOWN, self.OnRightClick)
29
30 def OnLeftClick(self, event):
31 self.leftClick = True
32 self.OnClick()
33 event.Skip()
34
35 def OnRightClick(self, event):
36 self.rightClick = True
37 self.OnClick()
38 event.Skip()
39
40 def OnClick(self):
41 self.clickCount += 1
42 if self.leftClick and self.rightClick:
43 self.leftClick = False
44 self.rightClick = False
45 evt = TwoButtonEvent(myEVT_TWO_BUTTON, self.GetId())
46 evt.SetClickCount(self.clickCount)
47 self.GetEventHandler().ProcessEvent(evt)
48
49
50class CustomEventFrame(wx.Frame):
51 def __init__(self, parent, id):
52 wx.Frame.__init__(self, parent, id, 'Click Count: 0',
53 size=(300, 100))
54 panel = TwoButtonPanel(self)
55 self.Bind(EVT_TWO_BUTTON, self.OnTwoClick, panel)
56
57 def OnTwoClick(self, event):
58 self.SetTitle("Click Count: %s" % event.GetClickCount())
59
60if __name__ == '__main__':
61 app = wx.PySimpleApp()
62 frame = CustomEventFrame(parent=None, id=-1)
63 frame.Show()
64 app.MainLoop()