Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

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.

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top