Как закрыть дочернее окно?

def press1 (self):
    self.destroy()

def press ():
    self  = Toplevel(tk)
    self.geometry("500x500")
    self.title("2 окно ")
    self.resizable(width=False, height=False)
    tk.withdraw()
    bl = Button(self,text = "закрыть",command = press1)
    bl.place(x=1,y=1, width=200, height=120)
    bl.pack


from tkinter import *

tk=Tk()
tk.title("test")
tk.geometry("500x500")
tk.resizable(width=False, height=False)
b2 = Button(tk,text = "next",command = press).place(x=100,y=100, width=120, height=50)
tk.mainloop()

Ответы (1 шт):

Автор решения: S. Nick

Попробуйте так:

def press1 (self, root):
    print(f'{self}') 
    root.destroy()

def press():
    self  = Toplevel(tk)
    self.geometry("500x500")
    self.title("2 окно ")
    self.resizable(width=False, height=False)
    tk.withdraw()

#-    bl = Button(self,text = "закрыть",command = press1)

# так:
#    bl = Button(self,text = "закрыть", command=lambda self=self, root=tk: press1(self, root))
# или так:
    bl = Button(self,text = "закрыть", command=lambda: tk.destroy())
        
    bl.place(x=1,y=1, width=200, height=120)
    bl.pack


from tkinter import *

tk = Tk()
tk.title("test")
tk.geometry("500x500")
tk.resizable(width=False, height=False)
b2 = Button(tk, text="next", command=press).place(x=100, y=100, width=120, height=50)
tk.mainloop()
→ Ссылка