1

To start off, let me show you my code:

import Tkinter
import tkMessageBox
import time
import sys

def endProgam():
    raise SystemExit
    sys.exit()

top = Tkinter.Tk()
B = Tkinter.Button(top, text = "Hello", command = endProgam)
B.pack()
top.mainloop()

As you can see under endProgram() I have tried 2 types of exit commands, both do not work. I never used them together, I was just trying to show what methods I have used so far. These methods were methods I found here and on other websites, but if I try either, I get this error:

Traceback (most recent call last):
  File "C:\Users\Sa'id\Documents\Learning Programming\Python\Tkinter Tuts.py", line 22, in <module>
    top.mainloop()
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1070, in mainloop
    self.tk.mainloop(n)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1488, in __call__
    raise SystemExit, msg
SystemExit

I can't seem to find a fix to this and I was hoping maybe someone here could help me. If you need any more details I will gladly provide what you need.

4
  • 2
    I can't reproduce the problem using your code as shown (with Python 2.7.8 on Win7-x64). How are you running the script?
    – martineau
    Commented Oct 17, 2014 at 2:05
  • Are you saying you get the "raise SystemExit" error message even when you call sys.exit()? Commented Oct 17, 2014 at 11:17
  • @martineau, I am also running 2.7(.8) and I am using Sublime Text 3 with SublimeREPL to run my Python programs. The question has been answered, thanks for trying to help.
    – said
    Commented Oct 17, 2014 at 23:31
  • @BryanOakley, Yes, both times I get the same errors. It was answered, thanks for trying to help.
    – said
    Commented Oct 17, 2014 at 23:35

1 Answer 1

6

There are two functions you should use to quit a window:

  • destroy()
  • quit()

Here you have the code using one of the two:

import Tkinter
import tkMessageBox
import time
import sys

def endProgam():
    # top.quit()
    top.destroy()        

top = Tkinter.Tk()

B = Tkinter.Button(top, text = "Hello", command = endProgam)
B.pack()
top.mainloop()
1
  • Thanks, top.destroy() worked but top.quit() only half worked, in the sense that SublimeREPL was terminated but the window was still there. Again, thanks.
    – said
    Commented Oct 17, 2014 at 23:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.