0

I'm trying to do something when a window is manually closed in tkinter and don't know a method/function to do so. A simple example of what im trying to do in a smaller scale.

from tkinter import *

root = Tk()
rootIsClosed = False # how to do this? How to make it True?
if rootIsClosed:     # ?
    new_win = Tk()
    x = Label(new_win, text='why did you close the program').pack()

1 Answers1

0

You can look at this answer which talks about how to handle exiting a window.

In short, you can do this:

from tkinter import *
root = Tk() #create new root

def on_close():
    #this is the code you provided

    new_win = Tk()
    x = Label(new_win, text='why did you close the program').pack()

root.protocol("WM_DELETE_WINDOW", on_close) # main part of the code, calls on_close
root.mainloop()

Note that this doesn't allow you to actually close the window, so you can do this to close the window:

from tkinter import *
root = Tk()

def on_close():
    new_win = Tk()
    x = Label(new_win, text='why did you close the program').pack()
    root.destroy() #only difference, root.destroy closes the original window.

root.protocol("WM_DELETE_WINDOW", on_close)
root.mainloop()
PythonPikachu8
  • 359
  • 2
  • 11