Python tkinter Toplevel for Child window

Tkinter Data Transfer between windows
We can create pop-up window with all functionality by using Toplevel in Tkinter.
Tkinter Toplevel child and parent windows

Tkinter Toplevel to open child window on button click and to close child window from parent & child


Here is one sample code to create two windows from starting.
import tkinter as tk
from tkinter import * 

my_w = tk.Tk()
my_w.geometry("200x200")  # Size of the window 
my_w.title("www.plus2net.com")  # Adding a title

# create one lebel 
my_str = tk.StringVar()
l1 = tk.Label(my_w,  textvariable=my_str )
l1.grid(row=1,column=2) 
my_str.set("Hi I am main window")

# child window 
my_w_child=Toplevel(my_w) # Child window 
my_w_child.geometry("200x200")  # Size of the window 
my_w_child.title("www.plus2net.com")

my_str1 = tk.StringVar()
l1 = tk.Label(my_w_child,  textvariable=my_str1 )
l1.grid(row=1,column=2) 
my_str1.set("Hi I am Child window")

my_w.mainloop()
To manage layout, read more on grid()

Opening the Child window on Button click

Button to open child window from parent windows
Above code will open both the windows ( parent and child ) at the beginning. We will change this by keeping the code for child window inside a function my_open(). We will use one button on parent window and click event of this will trigger the function my_open() to open the child window.
import tkinter as tk
from tkinter import * 
my_w = tk.Tk()
my_w.geometry("200x200")  # Size of the window 
my_w.title("www.plus2net.com")  # Adding a title

# create one lebel 
my_str = tk.StringVar()
l1 = tk.Label(my_w,  textvariable=my_str )
l1.grid(row=1,column=2) 
my_str.set("Hi I am main window")
# add one button 
b1 = tk.Button(my_w, text='Clik me to open new window',
               command=lambda:my_open())
b1.grid(row=2,column=2) 

def my_open():
    my_w_child=Toplevel(my_w) # Child window 
    my_w_child.geometry("200x200")  # Size of the window 
    my_w_child.title("www.plus2net.com")

    my_str1 = tk.StringVar()
    l1 = tk.Label(my_w_child,  textvariable=my_str1 )
    l1.grid(row=1,column=2) 
    my_str1.set("Hi I am Child window")
my_w.mainloop()

Child window with close buttons

Close Child and parent window from Child
Our child window can have two buttons, one button can close child window and other one can close parent window. While closing parent window child window will also close.
import tkinter as tk
from tkinter import * 
my_w = tk.Tk()
my_w.geometry("200x200")  # Size of the window 
my_w.title("www.plus2net.com")  # Adding a title

# create one lebel 
my_str = tk.StringVar()
l1 = tk.Label(my_w,  textvariable=my_str )
l1.grid(row=1,column=2) 
my_str.set("Hi I am main window")
# add one button 
b1 = tk.Button(my_w, text='Clik me to open new window',
               command=lambda:my_open())
b1.grid(row=2,column=2) 

def my_open():
    my_w_child=Toplevel(my_w) # Child window 
    my_w_child.geometry("200x200")  # Size of the window 
    my_w_child.title("www.plus2net.com")

    my_str1 = tk.StringVar()
    l1 = tk.Label(my_w_child,  textvariable=my_str1 )
    l1.grid(row=1,column=2) 
    my_str1.set("Hi I am Child window")
    b2 = tk.Button(my_w_child, text=' Close parent',
                   command=my_w.destroy)
    b2.grid(row=2,column=2) 

    b3 = tk.Button(my_w_child, text=' Close Child',
                   command=my_w_child.destroy)
    b3.grid(row=3,column=2)
my_w.mainloop()

Closing the child from Parent

This button is created inside the function my_open() along with the child window, however it is placed in the parent window.
def my_open():
    my_w_child=tk.Toplevel(my_w)
    my_w_child.geometry("200x200")
    my_w_child.title('www.plus2net.com')
    my_str1=tk.StringVar()
    l1=tk.Label(my_w_child,textvariable=my_str1)
    l1.grid(row=1,column=2)
    my_str1.set('Hi I am child window')
    b2=tk.Button(my_w_child,text='Close child',command=my_w_child.destroy)
    b2.grid(row=2,column=2)
    b3=tk.Button(my_w_child,text='Close Parent',command=my_w.destroy)
    b3.grid(row=4,column=2)
    b4=tk.Button(my_w,text='Close child',command=my_w_child.destroy)
    b4.grid(row=2,column=2)

Remove functionality of Parent from Child

From Tkinter Toplevel child window disable - enable parent window also hide and restore main window


On Button click we can remove all actions of Parent window my_w ( user is required to interact only with Child window my_w_child ) , here Parent window is available ( visible ) but disabled.

We are using grab_set() with click event of b3 button in Child window ( my_w_child )
b3 = tk.Button(my_w_child, text='Disable parent',
                   command=lambda:my_w_child.grab_set())
To restore the functionality of Parent window from child window we used grab_release().
b4 = tk.Button(my_w_child, text='Enable parent',
                   command=lambda:my_w_child.grab_release())
Full code about grab_set() and grab_release() is here

Hide & display Parent

Tkinter Toplevel Hide & display child and parent windows

To Make the Parent window invisible and then restore the same we can use withdraw()
b3 = tk.Button(my_w_child, text='Withdraw',
                   command=lambda:my_w.withdraw())
To restore the parent window we can use deiconify().
b4 = tk.Button(my_w_child, text='Deiconify',
	command=lambda:my_w.deiconify())
Full code about withdraw() and deiconify() is here

config

We can add options to our child window by using config. Here is a button to change the background colour of the child window.
b4 = tk.Button(my_w_child, text='Red',
               command=lambda:my_w_child.config(bg='red'))
We can add one button from child on Parent. By controling the button at Parent we can change colour of Child window. Similarly by controling the button at Child we can change colour of Parent window.
    # change background color of parent from child
    b4 = tk.Button(my_w_child, text='Parent Colour',
		command=lambda:my_w.config(bg='red'))
    b4.grid(row=4,column=2)
    # change background color of child from parent 
    b5 = tk.Button(my_w, text='Child Colour',
		command=lambda:my_w_child.config(bg='green'))
    b5.grid(row=5,column=2)
Similarly other options are here

bg=Background color
bd=Border width
colormap=color map to use
container=container
cursor=style of mouse pointer
height=window height
highlightbackground=highlight the region when not in focus
highlightcolor=colour when in focus

Toplevel without title bar

We can add this line to remove title bar. Use this line inside function my_open() after declaring my_w_child
my_w_child.wm_overrideredirect(True)
View & Download tkinter-toplevel ipynb file (.html format)

Passing data from Parent window to Child window Showing Message before Closing of Child window
Subscribe to our YouTube Channel here

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2022 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer