wxPython: MenuBar Bound Events Execute on Init
Question: Hello StackOverflow Hive Mind;

I am in the process of learning how to use wxPython it make a GUI for an
application I am writing. I've been slogging through that well enough until I
noticed that I could not properly make a menu bar. Whatever I am doing wrong
is causing my event bindings to be executed upon the running of the program.
Here is the code in question. Minor misspellings should be ignored, I am
retyping this off of my development machine which is offline so I might miss a
key here or there.

    
    
    def onDocClick():
       message = wx.MessageBox("did it work?","Question",wx.YES_NO)
    
    def onAboutClick():
       pass
    
    frame = wx.Frame(parent=None,title="RVR GUI",size=(315,490))
    frame.Show()
    panel = wx.Panel(frame,-1,size=(315,490)
    
    menubar = wx.MenuBar()
    fileMenu = wx.Menu()
    documentation = fileMenu.Append(wx.ID_NEW,"Documentation")
    about = fileMenu.Append(wx.ID_ABOUT,"About")
    menuBar.Append(fileMenu,'File')
    
    frame.SetMenuBar(menubar)
    menubar.Bind(wx.EVT_MENU,onDocClick(),documentation)
    menubar.Bind(wx.EVT_MENU,onAboutClick(),about)
    

There is alot more to the GUI than that but the rest of the code works fine so
I don't think you need it. Basically what is happening, is that the message
box in onDocClick() is executed as soon as I run the script that opens the
GUI. One I click amy button the rest of the GUI initializes fine and a click
on the "Documentation" menu entry sees absolutly nothin at all happen. I have
a feeling I am probobly missing something simple, perhaps wx.EVT_MENU is not
the right one to use in this situation? If that is the case what would the
right one to use be? Every single tutorial I have seen on wxPython has done it
this way without explaination. Honestly I have been appalled at the lack of
meaningful documentation for wxPython. The Zetcode tutorials and even the ever
helpful Mouse vs Python just seems to glaze right over the fundamentals of how
the toolkit works. But I digress and I've been making do, I've just been
completely befuddled by this. Anyway thanks and I appreciate any responses!

Michael


Answer: You should have a really close look at your code. I think you are doing
everything alright in terms of using wxPython, but the event binding is not
done properly. You should pass _method names_ as arguments for the `Bind`
method. Instead, you make a _method call_ (this causes the bound methods to be
executed during UI construction). To fix it, you have to leave out the
parentheses. The last two lines of your listing should become this:

~~

    
    
    menubar.Bind(wx.EVT_MENU, onDocClick, documentation)
    menubar.Bind(wx.EVT_MENU, onAboutClick, about)
    

~~

I tried out your code, and BrtH was right, I was not successul with binding
from the menubar, you should bind events from the `wx.Frame` instance instead.
Your complete, runnable example now looks like this:

    
    
    import wx
    
    def onDocClick(evt):
       message = wx.MessageBox("did it work?","Question",wx.YES_NO)
    
    def onAboutClick(evt):
       print "clicked"
    
    app = wx.App()
    frame = wx.Frame(parent=None,title="RVR GUI",size=(315,490))
    frame.Show()
    panel = wx.Panel(frame,-1,size=(315,490))
    
    menubar = wx.MenuBar()
    fileMenu = wx.Menu()
    documentation = fileMenu.Append(wx.ID_NEW,"Documentation")
    about = fileMenu.Append(wx.ID_ABOUT,"About")
    menubar.Append(fileMenu,'File')
    
    frame.SetMenuBar(menubar)
    frame.Bind(wx.EVT_MENU,onDocClick,documentation)
    frame.Bind(wx.EVT_MENU,onAboutClick,about)
    app.MainLoop()
    

