Is it possible to pass arguments into event bindings?

夙愿已清 提交于 2019-12-17 10:21:22

问题


I haven't found an answer elsewhere and this doesn't appear to have been asked yet on SO.

When creating an event binding in wxPython, is it possible to pass additional arguments to the event? For example, this is the normal way:

b = wx.Button(self, 10, "Default Button", (20, 20))
        self.Bind(wx.EVT_BUTTON, self.OnClick, b)
def OnClick(self, event):
        self.log.write("Click! (%d)\n" % event.GetId())

But is it possible to have another argument passed to the method? Such that the method can tell if more than one widget is calling it but still return the same value?

It would greatly reduce copy & pasting the same code but with different callers.


回答1:


You can always use a lambda or another function to wrap up your method and pass another argument, not WX specific.

b = wx.Button(self, 10, "Default Button", (20, 20))
        self.Bind(wx.EVT_BUTTON, lambda event: self.OnClick(event, 'somevalue'), b)
def OnClick(self, event, somearg):
        self.log.write("Click! (%d)\n" % event.GetId())

If you're out to reduce the amount of code to type, you might also try a little automatism like:

class foo(whateverwxobject):
    def better_bind(self, type, instance, handler, *args, **kwargs):
        self.Bind(type, lambda event: handler(event, *args, **kwargs), instance)

    def __init__(self):
        self.better_bind(wx.EVT_BUTTON, b, self.OnClick, 'somevalue')



回答2:


The nicest way would be to make a generator of event handlers, e.g.:

def getOnClick(self, additionalArgument):
    def OnClick(event):
        self.log.write("Click! (%d), arg: %s\n" 
                         % (event.GetId(), additionalArgument))
    return OnClick

Now you bind it with:

b = wx.Button(self, 10, "Default Button", (20, 20))
b.Bind(wx.EVT_BUTTON, self.getOnClick('my additional data'))


来源:https://stackoverflow.com/questions/173687/is-it-possible-to-pass-arguments-into-event-bindings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!