wxPython automatically does event at startup when it shouldn't
Question: So, I am pretty new to programming. I am currently making a simple text editor
(claaaassic) in wxPython on Mac. I have a the "Save" menu item bound to the
event saveFile() which takes a custom class file() as a parameter for the
directory and file type of the designated file. Everything looks normal, but
for some reason, the method is called automatically at startup, and doesn't
run when I actually clicked the Save menu item. Any help would be much
appreciated!

Also, the code below is just a sample of my program.

    
    
    import wx
    
    # Custom class file
    class file():
        def __init__(self, directory, file_type):
            self.directory = directory #location of file
            self.file_type = file_type #type of file (text, HTML, etc.)
    
    # Main Frame
    class Frame1(wx.Frame):
        def __init__(self, *args, **kwargs):
            wx.Frame.__init__(self, None, -1, 'Text Program', size=(500, 700))
            self.Bind(wx.EVT_CLOSE, self.Exit)
            self.f1 = file("/Users/Sasha/Desktop/File.txt", "Text File")   # an example file
            self.InitUI()
    
        def InitUI(self):
            self.text = wx.TextCtrl(self, style=wx.TE_MULTILINE)
            self.CreateStatusBar()
            # the reason we use "self.text" instead of just "text", is because if we want to use multiple windows, I believe
            self.menu()
            # Last. Centers and shows the window.
            self.Centre()
            self.Show()
    
        # Save file, WHERE I HAVE PROBLEMS
        def saveFile(self, a, file):
            directory = file.directory
            file_type = file.file_type
            file = open(directory, "r+") #r+ is reading and writing, so if file is same, no need to write
            print file.name + ",", file_type
            l = file.read()
            print l
            file.close()
    
        # Exit method, pops up with dialog to confirm quit
        def Exit(self, a):
            b = wx.MessageDialog(self, "Do you really want to close this application?", 'Confirm Exit', wx.CANCEL | wx.OK)
            result = b.ShowModal()
            if result == wx.ID_OK:
                self.Destroy()
            b.Destroy()
    
        def menu(self):
            # A shortcut method to bind a menu item to a method
            def bindMethod(item, method):
                self.Bind(wx.EVT_MENU, method, item)
    
            # FILE
            fileMenu = wx.Menu()
            new = fileMenu.Append(wx.ID_NEW, "New")
            save = fileMenu.Append(wx.ID_SAVE, "Save")
            saveAs = fileMenu.Append(wx.ID_SAVEAS, "Save As")
            bindMethod(new, self.newFile)
            bindMethod(save, self.saveFile(self, self.f1))    # WHERE I MAY HAVE PROBLEMS
            bindMethod(saveAs, self.saveAs)
    
            menuBar = wx.MenuBar()
            menuBar.Append(fileMenu, "&File")  # Adding the "filemenu" to the MenuBar
            self.SetMenuBar(menuBar)  # Adding the MenuBar to the Frame content.
    
    
    if __name__ == '__main__':
        app = wx.App()
        frame = Frame1()
        app.MainLoop()
    


Answer: You call the method on the same line where you think you bind it. Instead of
passing the method, you pass the return value of the method called.

In this line, the second parameter is a method:

    
    
    bindMethod(new, self.newFile)
    

In this line, the method is called and second parameter is whatever is
returned, e.g. None:

    
    
    bindMethod(save, self.saveFile(self, self.f1)) 
    

When you bind methods, the parameters and not used, so you bind to the method
name and do not call it at that place. The wxPython should call the bound
method with correct parameters when event is fired.

That also probably means that you cannot use parameters that you have, as
callback for `wx.EVT_MENU` is given. You would need to create some kind of
file dialog to get those parameters in the `saveFile` method.

