Skip to content

Instantly share code, notes, and snippets.

@driscollis
Created April 1, 2016 14:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save driscollis/75e51b3ea6cc80d9a699ea4ac63e65c7 to your computer and use it in GitHub Desktop.
Save driscollis/75e51b3ea6cc80d9a699ea4ac63e65c7 to your computer and use it in GitHub Desktop.
Custom event with wxPython
import wx
import wx.lib.newevent
BbClickEvent, EVT_BB_CLICK = wx.lib.newevent.NewCommandEvent()
BbClickEvent = wx.NewEventType()
EVT_BB_CLICK = wx.PyEventBinder(BbClickEvent, 0)
########################################################################
class MyCustomEvent(wx.PyCommandEvent):
eventType = BbClickEvent
def __init__(self, windowID, obj):
wx.PyCommandEvent.__init__(self, self.eventType, windowID)
self.SetEventObject(obj)
class TestFrame(wx.Frame):
def __init__(self):
# build the window
wx.Frame.__init__(self, None, -1, "TestFrame", size=(300, 400))
self.scrollWin = wx.PyScrolledWindow( self, -1 )
self.scrollWin.Bind(wx.EVT_LEFT_UP, self.onMouseUp)
x = 20
y = 20
for i in range(5):
# add labels
stTxt = wx.StaticText(self.scrollWin, -1, "item #{}".format(i), pos=(x, y) )
_, h = stTxt.GetSize()
dy = h + 10
y += dy
# set events
stTxt.Bind(wx.EVT_LEFT_UP, self.onMouseUp)
stTxt.Bind(EVT_BB_CLICK, self.onClick)
# configure scrollbars
self.scrollWin.SetScrollbars( 0, dy, 0, y/dy+1 )
self.scrollWin.SetScrollRate( 1, 1 ) # Pixels per scroll increment
def onClick(self, evt):
print('onClick', evt.GetEventObject())
def onMouseUp(self, evt):
obj = evt.GetEventObject()
print('onMouseUp', obj)
wx.PostEvent(obj.GetEventHandler(), MyCustomEvent(obj.GetId(), obj))
if __name__ == '__main__':
test_app = wx.App(redirect=False)
myAppFrame = TestFrame()
myAppFrame.Show()
test_app.MainLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment