469,133 Members | 1,318 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,133 developers. It's quick & easy.

Error: unbound method in Tkinter class

I am trying to structure a Tkinter application with classes instead of
just with simple functions, but I'm not sure how to call methods from my
main class.

My main class is packetstreamApp(). Within that class I call various
methods, including drawGUI() and authorizeDump(). My problem comes when
I try to call authorizeDump from the Tkinter menu. Here is the code that
calls authorizeDump():

self.packetmenu.add_command(label="Start Network Monitor",
command=packetstreamApp.authorizeDump())

Here is the code for authorizeDump():

def authorizeDump(self):
##do stuff here

And here is the error message that comes when I try to call authorizeDump():

Traceback (most recent call last):
File "/Users/kevin/Programming/packetstream/packetstream-classes.py",
line 257, in <module>
app = packetstreamApp()
File "/Users/kevin/Programming/packetstream/packetstream-classes.py",
line 19, in __init__
self.drawGUI()
File "/Users/kevin/Programming/packetstream/packetstream-classes.py",
line 49, in drawGUI
self.packetmenu.add_command(label="Start Network Monitor",
command=packetstreamApp.authorizeDump())
TypeError: unbound method authorizeDump() must be called with
packetstreamApp instance as first argument (got nothing instead)

I don't know how to interpret this error message, and so can't implement
a fix. Can anyone shed light on what I'm doing wrong?

Thanks.

--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
Dec 9 '06 #1
2 1798
Kevin Walzer wrote:
I am trying to structure a Tkinter application with classes instead of
just with simple functions, but I'm not sure how to call methods from my
main class.

My main class is packetstreamApp(). Within that class I call various
methods, including drawGUI() and authorizeDump(). My problem comes when
I try to call authorizeDump from the Tkinter menu. Here is the code that
calls authorizeDump():

self.packetmenu.add_command(label="Start Network Monitor",
command=packetstreamApp.authorizeDump())
If that code is inside a method of packetstreamApp:

self.packetmenu.add_command(label="...", command=self.authorizeDump)

i. e. you have to pass a bound method (a method that knows about "its"
instance) and you must not call authorizeDump here (no trailing '()').

Of course I'm only guessing...

Peter
Dec 9 '06 #2
Kevin Walzer wrote:
I am trying to structure a Tkinter application with classes
instead of just with simple functions, but I'm not sure how to
call methods from my main class.

My main class is packetstreamApp().
I don't think so -- packetstreamApp() would be an (unbound)
instance. packetstreamApp without parentheses is the class.
Within that class I call various methods, including drawGUI() and
authorizeDump(). My problem comes when I try to call authorizeDump
from the Tkinter menu. Here is the code that calls
authorizeDump():

self.packetmenu.add_command(label="Start Network Monitor",
command=packetstreamApp.authorizeDump())
[...]
TypeError: unbound method authorizeDump() must be called with
packetstreamApp instance as first argument (got nothing instead)
Problem here: You call an instance method without having created an
instance (it's not bound to any instance, thus it's an unbound
method).

One fix is to first create an instance and then access the method:

1.
psApp = packetstreamApp()

2.
test = psApp.authorizeDump()
OR
test = packetstreamApp.authorizeDump(psApp)

Regards,
Björn

P.S.: Beware: If you want to pass functions like above, leave out
the parentheses or they will be executed immediately and their
return value will be passed.

--
BOFH excuse #389:

/dev/clue was linked to /dev/null

Dec 9 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Fuzzyman | last post: by
2 posts views Thread by brent.chambers | last post: by
14 posts views Thread by 7stud | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.