How to get the name of the Master Frame in Tkinter?

Tkinter is a popular Python library used for creating graphical user interfaces (GUIs). When developing complex GUIs with Tkinter, you may need to access various properties of GUI elements, including getting the name or title of the master frame.

In this article, we will explore different methods to obtain the name of the master frame in Tkinter, which can be useful for programmatic manipulation of the GUI structure.

Understanding Tkinter Frames

In Tkinter, a frame is a rectangular container used to organize and group other widgets. Each frame can have a unique name assigned to it using the name parameter, allowing for easy identification and manipulation.

Method 1: Using title() for Root Window

The most common approach is to get the title of the root window, which serves as the master frame ?

import tkinter as tk

# Create the root window (master frame)
root = tk.Tk()
root.geometry("400x200")
root.title("My Master Frame")

# Function to get master frame name
def get_master_frame_name():
    frame_name = root.title()
    print("Master Frame Name:", frame_name)

# Call the function
get_master_frame_name()

root.mainloop()
Master Frame Name: My Master Frame

Method 2: Using winfo_name() for Widget Name

You can also get the internal widget name using the winfo_name() method ?

import tkinter as tk

root = tk.Tk()
root.title("My Application")

# Get the widget name of root
widget_name = root.winfo_name()
print("Widget Name:", widget_name)

# Create a named frame
master_frame = tk.Frame(root, name="main_frame")
master_frame.pack()

# Get the frame's name
frame_widget_name = master_frame.winfo_name()
print("Frame Widget Name:", frame_widget_name)

root.mainloop()
Widget Name: .
Frame Widget Name: main_frame

Method 3: Accessing Custom Frame Names

When you create frames with custom names, you can retrieve them using widget methods ?

import tkinter as tk

root = tk.Tk()
root.title("Frame Name Demo")

# Create frames with custom names
frame1 = tk.Frame(root, name="header_frame")
frame2 = tk.Frame(root, name="content_frame") 
frame3 = tk.Frame(root, name="footer_frame")

frame1.pack(fill='x')
frame2.pack(fill='both', expand=True)
frame3.pack(fill='x')

# Function to get all frame names
def list_frame_names():
    print("Root title:", root.title())
    print("Root widget name:", root.winfo_name())
    
    for child in root.winfo_children():
        if isinstance(child, tk.Frame):
            print(f"Frame name: {child.winfo_name()}")

list_frame_names()
root.mainloop()
Root title: Frame Name Demo
Root widget name: .
Frame name: header_frame
Frame name: content_frame  
Frame name: footer_frame

Comparison of Methods

Method Returns Best For
title() Window title Getting user-friendly window name
winfo_name() Internal widget name Getting programmatic widget identifier
Custom naming Developer-assigned names Organizing complex GUI structures

Practical Example

Here's a practical example showing how to use frame names for dynamic GUI manipulation ?

import tkinter as tk

class FrameManager:
    def __init__(self):
        self.root = tk.Tk()
        self.root.title("Frame Manager Demo")
        self.root.geometry("400x300")
        
        # Create named frames
        self.create_frames()
        
    def create_frames(self):
        # Header frame
        self.header = tk.Frame(self.root, name="header", bg="lightblue")
        self.header.pack(fill='x', pady=5)
        
        tk.Label(self.header, text="Header Frame", bg="lightblue").pack()
        
        # Content frame  
        self.content = tk.Frame(self.root, name="content", bg="lightgreen")
        self.content.pack(fill='both', expand=True, pady=5)
        
        tk.Label(self.content, text="Content Frame", bg="lightgreen").pack()
        
    def show_frame_info(self):
        print("=== Frame Information ===")
        print(f"Master window title: {self.root.title()}")
        print(f"Master widget name: {self.root.winfo_name()}")
        
        for child in self.root.winfo_children():
            if isinstance(child, tk.Frame):
                print(f"Frame: {child.winfo_name()}")

# Create and run the application
app = FrameManager()
app.show_frame_info()
app.root.mainloop()
=== Frame Information ===
Master window title: Frame Manager Demo
Master widget name: .
Frame: header
Frame: content

Conclusion

Getting the name of the master frame in Tkinter can be accomplished using title() for window titles or winfo_name() for widget names. Use custom frame naming for better organization in complex applications.

Updated on: 2026-03-27T16:06:55+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements