Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
532 views
in Technique[技术] by (71.8m points)

python - Detect when the "x" or close button is pressed

How would you know when the user pressed the "x" button on the close button in a python tkinter program and do an event?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can override the closing protocol.

def on_close():

     #custom close options, here's one example:

     close = messagebox.askokcancel("Close", "Would you like to close the program?")
     if close:
          root.destroy()

root.protocol("WM_DELETE_WINDOW",  on_close)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...