Error: unbound method in Tkinter class

K

Kevin Walzer

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.
 
P

Peter Otten

Kevin said:
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
 
B

Bjoern Schliessmann

Kevin said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top