python 在python中销毁Toplevel tk窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/351821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:56:28  来源:igfitidea点击:

destroying a Toplevel tk window in python

pythontkinter

提问by Hortitude

I was trying to write code that would auto-close a Toplevel Tk window in Python.

我试图编写可以在 Python 中自动关闭 Toplevel Tk 窗口的代码。

I ended up getting it to work, but ran into a little problem along the way that I wasn't able to figure out.

我最终让它开始工作,但在此过程中遇到了一个我无法弄清楚的小问题。

The second two buttons work, but the first one doesn't and I don't understand why...

后两个按钮有效,但第一个无效,我不明白为什么......

Any ideas?

有任何想法吗?

from Tkinter import *

root = Tk()
def doDestroy ():
    TL.destroy()

TL = Toplevel()
TL.b = Button (TL, text="lambda destroy", command=lambda: TL.destroy)
TL.b.pack()

TL.b2 = Button (TL, text="callback destroy", command=doDestroy)
TL.b2.pack()

de = lambda: TL.destroy()
TL.b3 = Button (TL, text="lambda that works", command=de)
TL.b3.pack()
root.mainloop()

回答by Piotr Lesnicki

Because it returns a function and not its result.

因为它返回一个函数而不是它的结果。

You should put:

你应该把:

command=TL.destroy

or if you used lambda:

或者如果您使用 lambda:

command=lambda: TL.destroy()