关闭tkinter顶级窗口

时间:2019-05-09 10:31:03

标签: python python-3.x tkinter toplevel

如何在按下按钮后关闭tkinter顶级窗口?我已经在按钮上关联了一个命令,所以我无法销毁窗口。

我班上有多个顶级课程

1 个答案:

答案 0 :(得分:0)

如果您的顶部窗口按钮必须执行任务,然后退出,则可以在完成任务后调用top.destroy

也许是这样的:

import tkinter as tk

def do_that_and_close(top):
    print('doing that')
    top.destroy()

def launch_top():
    top = tk.Toplevel(root)
    lbl = tk.Label(top, text='this is top')
    lbl.pack()    
    btn = tk.Button(top, text='do this and quit', command=lambda: do_that_and_close(top))
    btn.pack()

root = tk.Tk()
lbl = tk.Label(root, text='this is root')
lbl.pack()
root_btn = tk.Button(root, text='launch top', command=launch_top)
root_btn.pack()
quit_btn = tk.Button(root, text='quit', command=root.destroy)
quit_btn.pack()

root.mainloop()