When I run this code:

import wx

ID_FILE_QUIT = 101

class MainFrame(wx.Frame):
    def __init__(self, title):
        wx.Frame.__init__(self, None, wx.ID_ANY, title=title)
        self.menuBar = wx.MenuBar()
        self.fileMenu = wx.Menu()
        self.fileMenu.Append(1, '&Quit\tCtrl+Q')
        self.Bind(wx.EVT_MENU, self.Close(), id=ID_FILE_QUIT)
        self.menuBar.Append(self.fileMenu, '&File')
        self.SetMenuBar(self.menuBar)
        self.Show()

app = wx.App()
frame = MainFrame('Ya Mum')
app.MainLoop()

I get some error about expecting a callable object for self.Bind()?

Can someone tell me why this is happening?

self.Bind(wx.EVT_MENU, self.Close(), id=ID_FILE_QUIT)

Theres ya problem!
You dont need to put the brackets after self.Close, the brackets are only used if you actually want to call the function, so take out those brackets and try again. That hopefully should fix your dilemma.

:)

If paulthom12345's suggestion does not fix your problem, try to bind this way:

import wx

ID_FILE_QUIT = 101

class MainFrame(wx.Frame):
    def __init__(self, title):
        wx.Frame.__init__(self, None, wx.ID_ANY, title=title)
        self.menuBar = wx.MenuBar()
        self.fileMenu = wx.Menu()
        self.fileMenu.Append(ID_FILE_QUIT, '&Quit\tCtrl+Q')
        self.Bind(wx.EVT_MENU, self.quit, id=ID_FILE_QUIT)
        self.menuBar.Append(self.fileMenu, '&File')
        self.SetMenuBar(self.menuBar)
        self.Show()
        
    def quit(self, event):
        self.Close(True)


app = wx.App()
frame = MainFrame('Ya Mum')
app.MainLoop()

Forgot to mention this, but I also corrected line 10 to:

self.fileMenu.Append(ID_FILE_QUIT, '&Quit\tCtrl+Q')

Thanks guys, it turns out that I had to only put the function name, not call the function. :)

Thanks guys, it turns out that I had to only put the function name, not call the function. :)

Better test it, that won't work!

Better test it, that won't work!

Why not? It worked for me when I tested his code... and there's nothing wrong with his binding as I added another menu item with a separate function and ID and that still worked without conflict.

Why not? It worked for me when I tested his code... and there's nothing wrong with his binding as I added another menu item with a separate function and ID and that still worked without conflict.

here is what I got:

File "c:\Users\Elijah Ministries\Desktop\daniweb.py", line 17, in <module>
  frame = MainFrame('Ya Mum')
File "c:\Users\Elijah Ministries\Desktop\daniweb.py", line 11, in __init__
  self.Bind(wx.EVT_MENU, self.Close(), id=ID_FILE_QUIT)
File "C:\Python25\Lib\site-packages\wx-2.8-msw-unicode\wx\_core.py", line 3911, in Bind
  event.Bind(self, id, id2, handler)
File "C:\Python25\Lib\site-packages\wx-2.8-msw-unicode\wx\_core.py", line 3985, in Bind
  target.Connect(id1, id2, et, function)
File "C:\Python25\Lib\site-packages\wx-2.8-msw-unicode\wx\_core.py", line 3868, in Connect
  return _core_.EvtHandler_Connect(*args, **kwargs)

I wonder how it runs on you!
What version of wxPython do you have?

If you were to take the most updated code, you'd see that it works. This is what Lardmeister posted:

import wx

ID_FILE_QUIT = 101

class MainFrame(wx.Frame):
    def __init__(self, title):
        wx.Frame.__init__(self, None, wx.ID_ANY, title=title)
        self.menuBar = wx.MenuBar()
        self.fileMenu = wx.Menu()
        self.fileMenu.Append(ID_FILE_QUIT, '&Quit\tCtrl+Q')
        self.Bind(wx.EVT_MENU, self.quit, id=ID_FILE_QUIT)
        self.menuBar.Append(self.fileMenu, '&File')
        self.SetMenuBar(self.menuBar)
        self.Show()
        
    def quit(self, event):
        self.Close(True)


app = wx.App()
frame = MainFrame('Ya Mum')
app.MainLoop()

Which tom tetlaw acknowledged as he said "all I need to do is put the function name, not call the function". This is what we're doing by binding the function self.quit, rather than self.quit().

Sorry, I was referring to using the line:
self.Bind(wx.EVT_MENU, self.Close, id=ID_FILE_QUIT)
rather than the original code line:
self.Bind(wx.EVT_MENU, self.Close(), id=ID_FILE_QUIT)

Lardmeister's code works fine.

Ohh ok, gotcha. You had me confused there for a moment :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.