Accessing child widgets of a window by their names? Accessing child widgets of a window by their names? tkinter tkinter

Accessing child widgets of a window by their names?


I'm not sure what you mean by names in:

"Is there any way in Python/tkinter to access child elements referring by their names?"

You can access widgets simply by their object references:

# Proceduralimport tkinter as tkdef change():    object_reference['text'] = "Changed!"root = tk.Tk()object_reference = tk.Label(root, text="This is a label for root-window")object_reference.pack()another_window = tk.Toplevel(root)btn_in_other_window = tk.Button(another_window, text="Change")btn_in_other_window['command'] = changebtn_in_other_window.pack()root.mainloop()

or if they were to be defined with more of an object-oriented approach, you can make use of the .(dot) notation:

#oopimport tkinter as tkclass App1(tk.Toplevel):    def __init__(self, master):        super().__init__()        self.label = tk.Label(self, text="This is App1's label")        self.label.pack()class App2(tk.Toplevel):    def __init__(self, master):        super().__init__(master)        self.button = tk.Button(self, text="This is a App2's button")        self.button.pack()def change(label):    label['text'] = "Changed!"if __name__ == '__main__':    root = tk.Tk()    root.withdraw()    app1 = App1(root)    app2 = App2(root)    app2.button['command'] = lambda label=app1.label: change(label)    root.mainloop()


winfo_children() returns an instance of the class associated with the type of widget along with the name that tkinter assigned to the actual tk object.

This means that yes, we can refer to the name of widget, although I'm not sure what advantage this would really give you other than not needing to assign the label to a variable.

from tkinter import *root = Tk()Label(root, text="Label1").pack()label2 = Label(root, name="name", text="Label2")label2.pack()print(root.winfo_children())print(root.nametowidget('.!label'))print(str(label2))Button(root, text="Delete label2", command=lambda: root.nametowidget(".name").destroy()).pack()

The above will result in two Label widgets and a Button widget appearing in the window. The first Label is not stored in a variable and yet we can quite happily call it inside the print statement. The second is stored in a variable but you can see that in the command of the Button we don't refer to the variable but the name attribute of the Label.

Bryan Oakley has a fantastic answer here explaining this.


Not an exact answer to your questions, because as mentioned in the comments tkinter elements do not have names.

However it is possible to get a list of the children widgets of the element window with:

root.update()lista = list(root.children.values()) 

Then it is possible to refer to the list elements and do whatever with the widget itself. For example to get the width of the first widget do print(lista[0].winfo_width()).

Warning: I am not sure that the list contains the elements in the same order that the widgets appear in the script, although for me it worked in this order. Hopping someone will write in the comments.