Reputation: 197
My program has to do some task after the tkinter
window is closed.
When the 'X' button is pressed, it should print "The window is closed, how to do it"???
my code is
from tkinter import *
root = Tk()
Label(root, text = "This is for stackoverflow('X' button clicked code)").pack()
root.mainloop()
Actually I am looking for something like return value...
Upvotes: 0
Views: 2185
Reputation: 3011
Try printing the statement after the mainloop()
ends. Look at @BryanOakley's comment -
root.mainloop()
print("The window is closed")
The print statement would not run until you close the window or when the mainloop ends
Edit - Maybe something like this will work -
import tkinter as tk
root = tk.Tk()
l = tk.Label(root,text='hiiii')
l.pack()
root.mainloop()
print('The application is closed, new window opening')
win = tk.Tk()
l = tk.Label(win,text='byeee')
l.pack()
win.mainloop()
Upvotes: 0
Reputation:
You can use WM_DELETE_WINDOW
protocol to detect if root window is closed.
root.protocol("WM_DELETE_WINDOW", lambda: print ("The window is closed."))
Also, mainloop()
is a sort of while loop which keeps the GUI running.
You can also add print('The window is closed.')
after it
root.mainloop()
print('The window is closed.')
Upvotes: 2