Tkinter widgets: Write options *and* option tags dynamically from dictionary?
Laura Creighton
lac at strakt.com
Mon Feb 4 10:09:59 EST 2002
> Hello Laura and Eric,
>
> thanks very much for your answer, it has helped me a lot :)
Good to hear it.
>
> Laura, I don't think,
>
> widget=widgetType(master, **mydict)
>
> is necessary. I have tried
>
> widget=widgetType(master, mydict)
>
> with mydict={'text':'hello', 'relief':'sunken'}. It worked without the
> two stars.
> Aren't the two stars just necessary for function calls with variable
> length key parameters?
>
>
> ByeBye
>
> Johannes
>
Fredrik Lundh's Introduction to Tkinter has a warning about this:
http://www.pythonware.com/library/tkinter/introduction/x386-backwards-compatibility.htm
Quoting Fredrik Lundh here:
For example, if you create a custom widget which needs to pass
configuration options along to its parent class, you may come up
with something like:
def __init__(self, master, **kw):
Canvas.__init__(self, master, kw) # kw is a dictionary
This works just fine with the current version of Tkinter, but it may
not work with future versions. A more general approach is to use
the apply function:
def __init__(self, master, **kw):
apply(Canvas.__init__, (self, master), kw)
--------
The ** syntax is just a convenient shorthand for the apply. It won't
work before Python 2.0 and you will have to use the apply form.
apply is documented here http://www.python.org/doc/lib/built-in-funcs.html
Laura Creighton
More information about the Python-list
mailing list