Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to create a child window and communicate with parents in Tkinter?
Tkinter provides powerful features for creating multi-window applications. You can create child windows (also called dialog boxes) that communicate with their parent windows to share data and respond to user interactions.
A child window in Tkinter is created using the Toplevel() constructor, which creates a new top-level window that can interact with its parent window.
Basic Child Window Creation
Here's how to create a simple child window and pass data from parent to child ?
import tkinter as tk
from tkinter import ttk
# Create main window
root = tk.Tk()
root.geometry("400x200")
root.title("Parent Window")
def open_child_window():
# Create child window
child = tk.Toplevel(root)
child.geometry("300x150")
child.title("Child Window")
# Get data from parent and display in child
user_input = entry.get()
tk.Label(child, text=f"You entered: {user_input}",
font=('Arial', 12)).pack(pady=20)
# Create widgets in parent window
tk.Label(root, text="Enter some text:", font=('Arial', 11)).pack(pady=10)
entry = ttk.Entry(root, width=30)
entry.pack(pady=5)
button = ttk.Button(root, text="Open Child Window", command=open_child_window)
button.pack(pady=20)
root.mainloop()
Two-Way Communication Example
This example shows how to send data from child back to parent window ?
import tkinter as tk
from tkinter import ttk
class ParentChildApp:
def __init__(self):
self.root = tk.Tk()
self.root.geometry("400x250")
self.root.title("Parent Window")
# Parent window widgets
tk.Label(self.root, text="Parent Window",
font=('Arial', 14, 'bold')).pack(pady=10)
self.parent_entry = ttk.Entry(self.root, width=30)
self.parent_entry.pack(pady=5)
ttk.Button(self.root, text="Send to Child",
command=self.open_child).pack(pady=10)
self.result_label = tk.Label(self.root, text="Result from child will appear here",
fg='blue', font=('Arial', 10))
self.result_label.pack(pady=10)
def open_child(self):
# Create child window
self.child = tk.Toplevel(self.root)
self.child.geometry("350x200")
self.child.title("Child Window")
# Display data from parent
parent_data = self.parent_entry.get()
tk.Label(self.child, text=f"From parent: {parent_data}",
font=('Arial', 11)).pack(pady=10)
# Child input widget
tk.Label(self.child, text="Enter response:").pack(pady=5)
self.child_entry = ttk.Entry(self.child, width=25)
self.child_entry.pack(pady=5)
# Button to send data back to parent
ttk.Button(self.child, text="Send to Parent",
command=self.send_to_parent).pack(pady=10)
ttk.Button(self.child, text="Close",
command=self.child.destroy).pack(pady=5)
def send_to_parent(self):
# Get data from child and update parent
child_data = self.child_entry.get()
self.result_label.config(text=f"Child sent: {child_data}")
self.child.destroy()
def run(self):
self.root.mainloop()
# Run the application
app = ParentChildApp()
app.run()
Key Features
| Method | Purpose | Usage |
|---|---|---|
Toplevel(parent) |
Create child window | Basic window creation |
withdraw() |
Hide parent window | Modal-like behavior |
destroy() |
Close window | Clean window closure |
grab_set() |
Make window modal | Force user interaction |
Modal Dialog Example
Creating a modal dialog that blocks interaction with the parent ?
import tkinter as tk
from tkinter import ttk
def create_modal_dialog():
# Create modal dialog
dialog = tk.Toplevel(root)
dialog.geometry("300x150")
dialog.title("Modal Dialog")
# Make it modal (blocks parent interaction)
dialog.grab_set()
dialog.transient(root) # Keep on top of parent
tk.Label(dialog, text="This is a modal dialog",
font=('Arial', 12)).pack(pady=20)
def close_dialog():
dialog.grab_release() # Release modal grab
dialog.destroy()
ttk.Button(dialog, text="Close", command=close_dialog).pack(pady=10)
# Create main window
root = tk.Tk()
root.geometry("400x200")
root.title("Main Window")
tk.Label(root, text="Click button to open modal dialog",
font=('Arial', 12)).pack(pady=30)
ttk.Button(root, text="Open Modal Dialog",
command=create_modal_dialog).pack(pady=20)
root.mainloop()
Conclusion
Use Toplevel() to create child windows that can communicate with their parent. For modal behavior, use grab_set() and transient() to control user interaction flow between windows.
