Tkinter widgets: Write options *and* option tags dynamically from dictionary?

Eric Brunel eric.brunel at pragmadev.com
Fri Feb 1 08:54:24 EST 2002


Hi Johannes,

> I want to create widgets dynamically. Therefore I have created a
> dictionary for the options (well, it is a list of lists of lists which
> have this dictionary included, but I will keep it simple here). The
> problem is that not only the options are unknown at compile time, but
> also the *option tags*. For example in Button(root, text='Hello') also
> the *text* option tag should be taken from the keys of the options
> dictionary.

Here are two simple solutions:

- If I remember well, all Tkinter widgets accept configuration at creation
time using either named parameters, or a dictionary. So doing:
b = Button(root, text='Hello')
or:
b = Button(root, {'text': 'Hello'})
should have the same result (notice the quotes around 'text'...)

- If it doesn't succeed, here's a solution that will work: just call the
"config" method not directly, but using "apply":
b = Button(root)
apply(b.config, (), {'text' : 'Hello'})
The empty tuple () in the second argument just means "no positional
parameters". The last argument gives the named parameters as a dictionary
(again, notice the quotes around 'text'). So the last line above is the
exact equivalent of:
b.config(text='Hello')

HTH
 - eric -





More information about the Python-list mailing list