Python 如何实现关闭当前子窗口

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 如何实现关闭当前子窗口相关的知识,希望对你有一定的参考价值。

我创建了一个主窗口,同时又有一个 子窗口。
主窗口上右键菜单可以直接关闭所有窗口,
那么怎么实现,在子窗口上右键菜单,关闭当前这个子窗口?
import tkinter as tk

root = tk.Tk()
root.geometry('300x300+10+10')

def root_x():
root.quit()

root_bar = tk.Menu(root)
root_bar.add_command(label = '关闭——窗口',command = root_x)
def root_menu(event):
root_bar.post(event.x_root,event.y_root)

root.bind('<Button-3>', root_menu)

root2 = tk.Toplevel()
root2.geometry('300x300+350+10')

def root2_x():
root2.quit()

root2_bar = tk.Menu(root2)
root2_bar.add_command(label = '关闭——窗口',command = root2_x)
def root2_menu(event):
root2_bar.post(event.x_root,event.y_root)

root2.bind('<Button-3>', root2_menu)

root.mainloop()

参考技术A

函数root2_x()改成:

def root2_x(): 

    root2.destroy() 



本回答被提问者采纳

以上是关于Python 如何实现关闭当前子窗口的主要内容,如果未能解决你的问题,请参考以下文章