简体   繁体   中英

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:

在此处输入图片说明

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

在此处输入图片说明

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM