Tkinter how to access the widget by name

Prasad, Ramit ramit.prasad at jpmorgan.com
Mon Oct 15 18:26:07 EDT 2012


???????? ????? wrote:
> I'm a little teapot ... himself the question: if I want to appeal to the widget, knowing his name... ?
> 
> # appropriated the name of the widget
> label = Label(frame, width = 40, text='text', name = 'name')
> ...
> name_='name'
> configure(name_)
> ...
> def configure(name_)
>         #And how can that be?
>         # At least let the text you want to change ....
> 
> I beg you ..!!!!
> --

I am unfamiliar with Tkinter, so this might not be very helpful.

Usually with the GUI I have created before I uses classes and store 
the widgets inside the classes. That makes it easier to use
`self.widgetname` or `getattr(self, widgetname)`. If that is not
something you can instead store the attributes in a list/dictionary.
In both cases make sure not to have multiple widgets created with
the same name.

Note the following is all untested and should be considered pseudo-code.

widgets = {}
label = Label(frame, width = 40, text='text', name = 'name')
widgets['name'] = label

def configure(name_):
    widget = widgets[name_] 

OR

widgets = []
label = Label(frame, width = 40, text='text', name = 'name')
widgets.append( label )

def configure(name_):
    found = False
    for w in widgets:
         if w.name == name_: # No idea how to get name from Tk widget
               found = True
               break
     if found:
        # configure here


Ramit Prasad


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  



More information about the Python-list mailing list