0

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.

AhmedWas
  • 1,205
  • 3
  • 23
  • 38
  • @downvoter, I know that the question is silly. But I can't find an explanation for this. I would say sharing your knowledge helps more than down voting. Thanks! – AhmedWas Sep 27 '18 at 10:55
  • 1
    You should never create more than one instance of `Tk`. If you need multiple windows, everyone but the first needs to be an instance of `Toplevel`. – Bryan Oakley Sep 27 '18 at 11:28
  • @BryanOakley, I've tried Toplevel for the second win instead of Tk, but I still get the same problem. – AhmedWas Sep 27 '18 at 12:58

1 Answers1

1

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.

AhmedWas
  • 1,205
  • 3
  • 23
  • 38