Toplevel
has the master
argument that specifies the parent window.
This allows you to create subwindows with subwindows as the parent.
sample code
from tkinter import*
from tkinter import ttk
w1 = Tk()
w1.geometry("240x64+100+100")
ttk.Label(w1,text="Close parent, child and grandchild close" ).grid(row=0, column=0)
# Specify w1 in parent window
w2 = Toplevel (master = w1)
w2.geometry("240x64+100+200")
ttk.Label(w2,text="Close child, grandchild will close" ).grid(row=0, column=0)
# Specify w2 in parent window
w3 = Toplevel (master = w2)
w3.geometry("240x64+100+300")
ttk.Label(w3,text="Closing a grandchild means closing a grandchild." ).grid(row=0, column=0)
w1.mainloop()
(Added after receiving comments)
You can use the code below to invoke the grandchild window with the buttons on the child window.
from tkinter import*
from tkinter import ttk
from functools import partial
w1 = Tk()
w1.geometry("240x64+100+100")
ttk.Label(w1,text="Close parent, child and grandchild close" ).grid(row=0, column=0)
# a function that increases the number of children when a button is pressed
def call_child(w_master:Misc):
# Specify w2 in parent window
w_child=Toplevel(master=w_master)
w_child.geometry("240x64")
ttk.Button(w_child, text="Call child.", command=partial(call_child, w_child)) .grid(row=0, column=0)
# Specify w1 in parent window
w2 = Toplevel (master = w1)
w2.geometry("240x64+100+200")
ttk.Label(w2,text="Close child, grandchild will close" ).grid(row=0, column=0)
ttk.Button(w2, text="Call child.", command=partial(call_child,w2)).grid(row=1, column=0)
# Specify w2 in parent window
w3 = Toplevel (master = w2)
w3.geometry("240x64+100+300")
ttk.Label(w3,text="Closing a grandchild means closing a grandchild." ).grid(row=0, column=0)
w1.mainloop()
© 2024 OneMinuteCode. All rights reserved.