是否可以将参数传递给事件绑定?

时间:2021-02-27 07:11:50

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

我没有在其他地方找到答案,而且似乎还没有在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:

在wxPython中创建事件绑定时,是否可以将其他参数传递给事件?例如,这是正常的方式:

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.

它会大大减少复制和粘贴相同的代码,但使用不同的调用者。

2 个解决方案

#1


43  

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

您始终可以使用lambda或其他函数来包装方法并传递另一个参数,而不是特定于WX的参数。

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


13  

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'))

#1


43  

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

您始终可以使用lambda或其他函数来包装方法并传递另一个参数,而不是特定于WX的参数。

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


13  

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'))