[Tutor] Help me : Why this code is not working??

Luke Paireepinart rabidpoobear at gmail.com
Fri Oct 13 01:53:51 CEST 2006


Asrarahmed Kadri wrote:
>  
>  
> I have created two buttons. The code for button2 is not working as I 
> want it to be. When I click button2, the application should exit, but 
> it isnt.
> Can someone fix it??
>
> from Tkinter import *
> from tkMessageBox import *
>
> def callback():
>     showinfo('message','I am here...')
>
> def QUIT():
>     ans = askyesno('Confirm','Do you really want to quit?')
>     if ans:
>         root.exit
>
SEND US THE TRACEBACK!!!! :)
Don't just tell us that something doesn't work.
The traceback contains valuable information that tells you (or us) how 
to debug it.
In this case,the traceback was
#//////
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python24\lib\lib-tk\Tkinter.py", line 1345, in __call__
    return self.func(*args)
  File "C:/Python24/temp.py", line 10, in QUIT
    root.exit
  File "C:\Python24\lib\lib-tk\Tkinter.py", line 1654, in __getattr__
    return getattr(self.tk, attr)
AttributeError: exit
#\\\\
Okay, so what do we see here?
Start reading from the bottom.
AttributeError: exit.
okay, so that means that we tried to access a method or a variable of a 
class,  and it wasn't  there.
It lacked that attribute.

Which class was it?
looking further up the stack, we see where we access 'exit.'
line 10, in QUIT:
root.exit

This means that whatever class root is an instance of doesn't have a 
method named exit.

Remember, the computer is stupid.  To the computer, 'exit' and 'quit' 
mean something as different
as 'root beer' and 'trash can' would to us.  The method you're trying to 
access is called 'quit', not 'exit'
That's your first problem.

The second problem you have, is that you're not calling this method, 
you're just accessing it, which doesn't really do anything.
What you'll want to do is
root.quit()

and not
root.quit

HTH,
-Luke


More information about the Tutor mailing list