AhmedWas
AhmedWas

Reputation: 1277

Closing a second Tkinter window doesn't work

Imagine the following very simple example:

from tkinter import *
from tempFunctions import *

startingWin = Tk()

button = Button(startingWin, text="Open Other Win", command=lambda: openSecondWin()).grid(row=0, column=0, padx=30, pady=30)

startingWin.mainloop()

The output is simply as following:

enter image description here

No if I click on the button, I open the second Win like:

enter image description here

The second window has the following code in tempFunctions.py:

from tkinter import *

def openSecondWin():

    secondWin = Tk()

    cancelButton = Button(secondWin, text="Cancel", command=secondWin.quit).grid(row=0, column=0, padx=30, pady=30)

    secondWin.mainloop()

I expect that when I press cancel, the secondWin should close. That doesn't happen. What I get is that when I click cancel, the second Win doesn't close. However, if click twice both windows (startingWin and secondWin) close together. Why?

Is there a logical explanation for this? Thanks!

UPDATE:

Trying with binding results in the same problem.

Also making the second win as Toplevel doesn't help.

Upvotes: 0

Views: 536

Answers (1)

AhmedWas
AhmedWas

Reputation: 1277

The problem was that I was using quit(). However, in case of multiple windows, one should use destroy() according to the answer here. That solved my problem.

Upvotes: 1

Related Questions