DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Python (http://www.daniweb.com/forums/forum114.html)
-   -   wxPython Error (http://www.daniweb.com/forums/thread200620.html)

tomtetlaw Jun 30th, 2009 4:15 am
wxPython Error
 
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?

Paul Thompson Jun 30th, 2009 4:35 am
Re: wxPython Error
 

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.

:)

Lardmeister Jun 30th, 2009 12:35 pm
Re: wxPython Error
 
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()

Lardmeister Jul 1st, 2009 1:34 pm
Re: wxPython Error
 
Forgot to mention this, but I also corrected line 10 to:

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

tomtetlaw Jul 2nd, 2009 7:55 pm
Re: wxPython Error
 
Thanks guys, it turns out that I had to only put the function name, not call the function. :)

vegaseat Jul 3rd, 2009 3:23 pm
Re: wxPython Error
 
Quote:

Originally Posted by tomtetlaw (Post 906936)
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!

shadwickman Jul 3rd, 2009 3:53 pm
Re: wxPython Error
 
Quote:

Originally Posted by vegaseat (Post 907717)
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.

evstevemd Jul 3rd, 2009 4:31 pm
Re: wxPython Error
 
Quote:

Originally Posted by
907747
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)

evstevemd Jul 3rd, 2009 4:33 pm
Re: wxPython Error
 
I wonder how it runs on you!
What version of wxPython do you have?

shadwickman Jul 3rd, 2009 4:35 pm
Re: wxPython Error
 
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().


All times are GMT -4. The time now is 12:58 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC