0

I am not sure my question is clear, so I'm going to explain a little more.

Here is the situation:

class Frame(wx.Frame):
    def __init__(self, title):
        [...]
        self.Bind(wx.EVT_CLOSE, self.onclose)

    def onclose(self, event):
        """
        Close the program
        """
        self.Destroy()

So, in this code, 'event' is useless, and I would like not to call for it then, but I haven't been able to do so. Is there a recommended use of 'event' I am not applying? Is it even possible not to call 'event' ? I have not found a way.

Thanks!

6
  • Now, when you say "call event", do you mean you have to include it in the definition of your handler? Commented May 2, 2014 at 19:58
  • I mean that I cannot do for example: def onclose(self): [...] Commented May 2, 2014 at 19:59
  • When you define onclose (your handler), event is in arguments list, as it must be. So what exactly are you asking? Commented May 2, 2014 at 20:02
  • Yes, event is in the arguments list. However, it is obviously not used in the function, thus making it a seemingly useless argument. I would like not to have useless arguments in my function, if possible. Commented May 2, 2014 at 20:04
  • Right, but as the answers state, when you Bind an event and a function, the event get sent to that function, so the function needs to expect it (even if you don't use it) Commented May 2, 2014 at 20:05

3 Answers 3

1

In this case, event is not used, but in other cases, event could be used to know how the function onclose was called; what event caused the function to execute. If your event was caused by a mouse click you could make onClose behave differently from a button click, using the same function.

event is required because any binding from wx will send an event object to the called function, hence when you cannot use def onclose(self) with accepting the event object.

Sign up to request clarification or add additional context in comments.

4 Comments

Okay. It just seems curious that it cannot be 'not called' when it is not used.
It is how wx defines it, so if you made your own function: def add(self,x,y), you would not be able to call it as add(1). wx forces any function that gets binded to accept an event the same way this add function forces you to provide two numbers.
Yes, but wouldn't it be possible for wx to set a default value in case 'event' is not given?
That's a good question. There might be other reasons that I don't know, but from my point of view, it is better to have all functions require it, since wx will always provide it and it will always be accessible for the user to disregard or not, to his preference.
0

The Bind method sends an event for practical reasons, so you can apply other methods like event.getposition() or event.getkey() and compare it in the called method.

You can call it anything you like btw, naming it event is just conventional.

1 Comment

I get that, I just find it annoying to have an argument that I won't use in that particular function. It's cool to apply those other methods, but in this function, I don't really care.
0

The wxPython event API requires that it sends an event to the event handler. Thus when you do the Bind, you are going to be sending some type of event to the event handler.

Here are some links that may help you understand wxPython better:

Since you do not want to follow the toolkit's API, you can abuse Python and do something like this:

import wx

########################################################################
class Example(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="test")
        panel = wx.Panel(self)

        btn = wx.Button(panel, label="Close")
        btn.Bind(wx.EVT_BUTTON, self.onClose)

    #----------------------------------------------------------------------
    def onClose(*args):
        """"""
        args[0].Destroy()

#----------------------------------------------------------------------
if __name__ == "__main__":
    app = wx.App(False)
    frame = Example()
    frame.Show()
    app.MainLoop()

This isn't recommended because you do not follow standard Python idioms by removing the reference to self in the onClose event handler. Of course, you are also removing the event from the method which is in violation of wxPython coding standards. But it does work!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.