Error: unbound method in Tkinter class

Bjoern Schliessmann usenet-mail-0306.20.chr0n0ss at spamgourmet.com
Sat Dec 9 17:33:22 EST 2006


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




More information about the Python-list mailing list