Child to Parent data transfer

Tkinter Toplevel
We will pass user entered data in one Entry area of child window. On click of button the user entered data will be passed to parent window and displayed as one existing string variable.
Pass data from child to parent windows

Passing data between child and parent window in Tkinter Toplevel

You can read basics of Parent and Child window here
We are using one string variable in parent window and assigned a string to it. In child window on click of a button the text entered at Entry box will transfer to parent 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("250x200")  # Size of the window 
    my_w_child.title("www.plus2net.com")

    l1 = tk.Label(my_w_child,  text='Your Name', width=10 ) 
    l1.grid(row=1,column=1) 

    e1 = tk.Entry(my_w_child, width=20,bg='yellow') 
    e1.grid(row=1,column=2)
    b2 = tk.Button(my_w_child, text='Submit',
               command=lambda:my_str.set(e1.get()))
    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()
View & Download tkinter-toplevel-data ipynb file (.html format)

Passing data from Parent to Child

Pass data from parent to child windows
We will place one Entry widget ( e2 ) at Parent window and while opening the child window the data entered inside the Entry widget will be displayed in the child window ( l3 ).

We will keep the Label at child window and it will read the data entered at Entry box of parent window
l3=tk.Label(my_w_child,text=e2.get())
    l3.grid(row=3,column=1)
Note here how the data is collected by using e2.get(). The code for Entry is here
e2=tk.Entry(my_w,width=10)
e2.grid(row=2,column=1)
Full code to transfer data from Parent to Child is here
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("250x200+200+50") 
l1_str=tk.StringVar()
font1=('Times',18,'bold')	
l1=tk.Label(my_w,textvariable=l1_str,font=font1)
l1.grid(row=1,column=1)
l1_str.set('Parent window')

e2=tk.Entry(my_w,width=10)
e2.grid(row=2,column=1)
b1=tk.Button(my_w,text='Open Child',command=lambda:my_open())
b1.grid(row=2,column=2)
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,text='Your name')
    l1.grid(row=1,column=1)
    e1=tk.Entry(my_w_child, bg='yellow')
    e1.grid(row=1,column=2)
    b2=tk.Button(my_w_child,text='Submit',
    command=lambda:l1_str.set(e1.get()))
    b2.grid(row=2,column=1)
    my_str1.set('Your Name')
    l3=tk.Label(my_w_child,text=e2.get())
    l3.grid(row=3,column=1)
   
my_w.mainloop()  # Keep the window open

Passing data from child window to Combobox in Parent

From Child window to transfer data to Combobox in Parent
We can keep one Combobox in Parent window and pass user entered data from Child window entry widget as option to Combobox.

Here we are assigning value to StringVar() my_str which is connected to Combobox cb1 in Parent window
import tkinter as tk
from tkinter import * 
from tkinter import ttk
my_w = tk.Tk()
my_w.geometry("400x300")  # Size of the window 
my_w.title("www.plus2net.com")  # Adding a title

# Add one Combobox 
my_str = tk.StringVar()
months=['google','MSN','Microsoft','Yahoo'] # options of Combobox 
cb1 = ttk.Combobox(my_w, values=months,width=10,textvariable=my_str,font=22)
cb1.grid(row=1,column=1,padx=10,pady=20)

# 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("250x200")  # Size of the window 
    my_w_child.title("www.plus2net.com")

    l1 = tk.Label(my_w_child,  text='Your Name', width=10 ) 
    l1.grid(row=0,column=0,padx=5,pady=10) 

    e1 = tk.Entry(my_w_child, width=20,bg='yellow',font=20) 
    e1.grid(row=1,column=0,padx=5,pady=2)

    b2 = tk.Button(my_w_child, text='Submit',
            command=lambda:my_str.set(e1.get())
    b2.grid(row=2,column=0,padx=5) 
    b3 = tk.Button(my_w_child, text=' Close Child',
                   command=my_w_child.destroy)
    b3.grid(row=3,column=0,padx=5)
    
my_w.mainloop()
Toplevel 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-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer