2012-01-17 19 views
5

Se creo un widget in Tkinter, posso specificare un nome di widget che prende parte al concetto di "percorso del widget" di tcl/tk. Ad esempio:È possibile cercare il widget per nome in Tkinter?

from Tkinter import * 
button = Button(Frame(Tk(), name = "myframe"), name = "mybutton") 
str(button) == ".myframe.mybutton" 

E 'possibile ottenere un widget con il suo nome, "mybutton" nel mio esempio?

risposta

7

Sì, ma è necessario tenere un riferimento alla root "Tk" istanza: basta usare il metodo "Tk.nametowidget":

>>> from Tkinter import * 
>>> win = Tk() 
>>> button = Button(Frame(win, name = "myframe"), name = "mybutton") 
>>> win.nametowidget("myframe.mybutton") 
<Tkinter.Button instance at 0x2550c68> 
3

widget di ogni Tkinter ha un attributo children che è un dizionario di widget namewidget instance. Dato che, si può trovare qualsiasi subwidget da:

widget.children['subwidget_name'].children['subsubwidget_name'] # ... 
Problemi correlati