If i create a widget in Tkinter i can specify a widget name that takes part in tcl/tk "widget path" concept. For example:
from Tkinter import *
button = Button( Frame( Tk(), name = "myframe" ), name = "mybutton" )
str( button ) == ".myframe.mybutton"
Is it possible to get a widget by it's name, "mybutton" in my example?
Yes, but you have to hold a reference to the root Tk
instance: just use the Tk.nametowidget()
method:
>>> from Tkinter import *
>>> win = Tk()
>>> button = Button( Frame( win, name = "myframe" ), name = "mybutton" )
>>> win.nametowidget("myframe.mybutton")
<Tkinter.Button instance at 0x2550c68>
Every Tkinter widget has an attribute children
which is a dictionary of widget name
→widget instance
. Given that, one can find any subwidget by:
widget.children['subwidget_name'].children['subsubwidget_name'] # ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With