简体   繁体   English

关闭第二个Tkinter窗口不起作用

[英]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: 不,如果我单击该按钮,则会像下面这样打开第二个Win:

在此处输入图片说明

The second window has the following code in tempFunctions.py: 第二个窗口在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. 我希望当我按取消时,secondWin应该关闭。 That doesn't happen. 那不会发生。 What I get is that when I click cancel, the second Win doesn't close. 我得到的是,当我单击“取消”时,第二个Win不会关闭。 However, if click twice both windows (startingWin and secondWin) close together. 但是,如果单击两次,则两个窗口(startingWin和secondWin)将关闭。 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() . 问题是我正在使用quit() However, in case of multiple windows, one should use destroy() according to the answer here . 但是,如果有多个窗口,则应根据此处的答案使用destroy() That solved my problem. 那解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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