Created
April 1, 2016 14:00
-
-
Save driscollis/75e51b3ea6cc80d9a699ea4ac63e65c7 to your computer and use it in GitHub Desktop.
Custom event with wxPython
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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