Reputation: 11580
While building my own widgets based on the stock ones, I've been adding a "name" field to every widget class of mine for easier access later, like:
class MyFrame(tk.Frame):
def __init__(self, master, name, *args, *kwargs):
super().__init__(master, *args, *kwargs)
self.name = name
After setting up the entire window layout, I could do something like:
mywidget = allWidgets['myWidgetName']
But I couldn't help but wonder if there is a better approach by using a widget's built-in attributes, without adding a new tag. I know that winfo_children()
helps traverse the widget tree, but I do need random access.
Upvotes: 1
Views: 66
Reputation: 11580
Based on @BryanOakley 's tip, I finally discovered that all Tkinter widgets have a ._name
attribute, whose default value is !<classname>
, all lowercase even if the classname uses mixed cases.
I could then assign any name to ._name
after instantiating my widget class.
It's understandable that this detail falls out of the official doc because this is not part of the public interface, but knowing this instantly saved memory for my own work.
However, I guess that it'd be my own responsibility to maintain the uniqueness of widget names if I started playing around with it.
I have to thank Bryan a million for his resourcefulness when it comes to Tkinter.
Upvotes: 2