Thread: Tkinter Exit

    #1
  1. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    588
    Rep Power
    64

    Unhappy Tkinter Exit


    I am playing around with Tkinter. How do you exit the Tk() form from the code, system.exit() doesn't wait and system.exit doesn't do it!
    Code:
    # a windows GUI form with one exit button
    # ouch, this one doesn't want to quit!!!!!!!!
    
    from Tkinter import *
    import sys
    
    root = Tk()  # create the window form
    btn = Button( text="Quit", command=sys.exit )
    btn.pack()   # pack the button into the form
    
    root.mainloop()  # run it
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Code:
    import sys
    sys.exit()
    I think wxPython has a Destroy function, I think it is used like so:
    Code:
    self.Destroy()
    Maybe Tkinter has a similar function. Check the Python Docs site for more information, I'm sure it will tell you. Other than that, after importing the sys module, sys.exit() SHOULD exit the application, if you're in a class, do sys.exit(self).

    Update: I got "unlazy" and actually looked at your code. You seem to have entered command=sys.exit where it should have command=sys.exit().
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    On tkinter it is parent.destroy

    Code:
    >>> from Tkinter import *
    >>> root = Tk()
    >>> Button(root,text="Quit",command=root.destroy).pack()
    >>> root.mainloop()
    Those people who think they know everything are a great annoyance to those of us who do.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Hi!

    root.quit does what you want. It quits the Tcl interpreter and destroys all widgets.

    Regards, mawe
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    588
    Rep Power
    64

    Smile


    Thanks for all the help!
    The jury is in ...
    Code:
    # a windows GUI form with one exit button
    # root.destroy works, root.quit does not
    
    from Tkinter import *
    
    root = Tk()  # create the window form
    btn = Button( text="Quit", command=root.destroy )
    btn.pack()   # pack the button into the form
    
    root.mainloop()  # run it
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Originally Posted by Dietrich
    root.destroy works, root.quit does not
    Strange. What happens if you use root.quit? Nothing, or is there any error message?
  12. #7
  13. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13

    Exclamation QUIT and DESTROY


    Mawe,
    quit will work if your program is called from a command line.

    As you say it tries to terminate the graphics and TCL interpreter - which seems to cause a lock-up if the program is launched within a graphical environment like IDLE or Pythonwin.

    destroy is better behaved - it kills only the resources you create. Calling destroy will cause the mainloop to terminate and deletes the widgets. Which is probably what most people want anyway.

    I've not found this irritating feature documented anywhere so it bites me from time to time when I forget and just look up terminating commands But now I've written it here I might just remember which command to use if future.

    grim
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Thanks for the explanation, grim. AFAIK, idle is a Tk-application, so there has to be some trouble if the Tcl-interpreter stops

IMN logo majestic logo threadwatch logo seochat tools logo