top of page

Overriding Tkinter “X” button control (the button that close the window) [duplicate]

If you are working with tkinter in python many a times user press x button and that lead to make you code crash while using sockets and stuff . So heres whta you can do


root.protocol('WM_DELETE_WINDOW',something)  

put this piece of code in your tkinter code .

this takes two arguments one is a x button detector and one is a function name before quitting the program.


There is the whole program with comments , how it work


# this is the main line detects x button pressed with a funtion name in the second argument  
root.protocol('WM_DELETE_WINDOW', doSomething)

#as soon as you press it this function gets activated 
doSomething():
    print("THE WINDOW WAS DESTROYED")
    root.destroy()
 

I will be happy if you make a discussion about new methods in comments

Comments


Post: Blog2 Post
bottom of page