Search code examples
pythonwxpython

passing event to function outside of class in wxPython


What do I have to do to handle an event with a function located outside of the class the event is triggered from. For example, the code below works fine when the onClick method is called when the panel is clicked.

class MyFrame(wx.Frame):

    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'Cubit 2D',size=(600,600))
        self.Center()
        self.panel=(wx.Panel(self))
        self.panel.SetBackgroundColour('grey')

        self.panel.Bind(wx.EVT_LEFT_DOWN,self.onClick)

    def onClick(self,event):
        print 'hello'

However, if I move the function onClick to a different .py file like shown below, it doesn't work. How do I pass the event info to the function located in the other file?

#main file

import wx
import onclick

class MyFrame(wx.Frame):

    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'Cubit 2D',size=(600,600))
        self.Center()
        self.panel=(wx.Panel(self))
        self.panel.SetBackgroundColour('grey')

        self.panel.Bind(wx.EVT_LEFT_DOWN,onclick.onClick)

other file with function

def onClick(self,event):
    print 'hello'

Solution

  • Just remove the self parameter from the onClick function in the other file:

    def onClick(event):
        print 'hello'
    

    You only need the self parameter for instance methods, not ordinary functions.