0

my first small GUI made with tkinter on python3.9 IDLE doesn't close. my code is:

import tkinter, sys

def ende():
    sys.exit(0)

window = tkinter.Tk()
anzeige = tkinter.Label(window, text = "Guten Tag")
knopf = tkinter.Button(window, text = "Ende", command = ende)
anzeige.pack()
knopf.pack()
window.mainloop()

a small GUI opens with a button "Ende" and when i click it nothing happens, even though i defined a function when clicking on it... why so? thanks for your help

Bryan Hain
  • 137
  • 1
  • 9
  • Take a look at this question, it will help you: https://stackoverflow.com/questions/110923/how-do-i-close-a-tkinter-window – HElooo Dec 13 '20 at 18:06

1 Answers1

0

Suppose your code is in tktest.py. Run it in from a command line with python tktest.py. The GUI appears. Click Ende and the command line prompt reappears.

Run it again with python -i tktest.py. The -i says "when the program stops, switch to interactive mode and give me a python '>>>' prompt so I can explore the state of the program". Click Ende and a traceback for SystemExit appears, followed by a >>> prompt. The GUI is still present, and you can enter dir() and other Python statements. Click Ende again and this time python exits. The command line prompt reappears.

When you run your code from an IDLE editor with F5, it imitates running with -i. A restart line and the GUI appear. When you click Ende, >>> appears. This is not 'nothing happens'. IDLE suppresses the SystemExit traceback. Hit Ende and another >>> prompt appears instead of IDLE exiting. This is all by design for user convenience.

Clicking the title bar [X] will close the gui. Another user already answered about a Close or Exit button.

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52