Getting every child widget of a Tkinter window

TkinterServer Side ProgrammingProgramming

In order to get the information about the widgets, tkinter provides a set of methods that can be used to test the widgets in an application. In order to get the list of all the child widgets, we can use the winfo_children() method.


In this example, we have defined some widgets in a frame and by using the winfo_children() method, we will print the list containing the names of all the widgets.

#Import tkinter library
from tkinter import *
#Create an instance of tkinter frame
win = Tk()
#Set the geometry
#Create a frame
frame= Frame(win)
#Create label, button widgets
Label(frame, text= "Top Level Text", font= ('Helvetica 20 bold')).pack(pady=20)
Button(frame, text= "Left").pack(side=LEFT)
Button(frame, text= "Right").pack(side= RIGHT)
Button(frame, text= "Center").pack(side= BOTTOM)
#Print the List of all Child widget Information


Executing the above code will display a window with a label and three buttons,

On the console, it will print the list of all the widgets defined in frame widget.

[<tkinter.Label object .!frame.!label>, <tkinter.Button object .!frame.!button>, <tkinter.Button object .!frame.!button2>, <tkinter.Button object .!frame.!button3>]
Updated on 15-Apr-2021 13:28:18