how to know if user closed a tkinter window?

Question:

how to know that user cliked on "X" button on top right corner of tkinter window and closed ( destroyed ) it? i want to use the result of this to destroy another while loop.

edit : i reviewed the suggested question but no , it does not answer my question because it’s about how to close a tkinter window

edit : the code:

import socket
from tkinter import *
import threading

win = Tk()
win.geometry("300x300")
win.resizable(False,False)


def disc() :
    s = socket.socket()
    ip = "0.0.0.0"
    port = 9999
    s.bind((ip,port))
    s.listen()
    print ('please wait...')
    c , addr =s.accept()
     print ('someone has joined!')


    while True :
        msg = input('your message : ' )
        c.send(msg.encode('utf8'))
        print (c.recv(1024).decode())

lbl_1 = Label(win,text="mychat",bg="light blue")
lbl_1.place(x=130,y=20)

word_1 = "hello"
word_2 = "hello2"

print(socket.gethostbyname(socket.gethostname()))

txt_1 = Text(win)
txt_1.pack()
txt_1.insert(END, word_1)
txt_1.insert(END, word_2)

connection = threading.Thread(target=disc).start()

win.mainloop()

it’s a messenger. here even after i close the window , the process remains but want to destroy the disc() process too.

Asked By: Parsa Ad

||

Answers:

If you want to continually check whether a window has been destroyed or not, you need a reference to the window. You can then call the winfo_exists method to determine if the window exists or not.

while True :
    ...
    if not win.winfo_exists():
        # window no longer exists; break out of loop
        break
Answered By: Bryan Oakley