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

Johannes Eble skywalkerpackage at hotmail.com
Wed Feb 6 07:25:33 EST 2002


Hello Eric,

thanks very much for your quick response. Now I have get the error
message:

Traceback (most recent call last):
  File
"D:\python\activestate\python21\Pythonwin\pywin\framework\scriptutils.py",
line 301, in RunScript
    exec codeObject in __main__.__dict__
  File "D:\python\activestate\python21\myscripts\labelentry_dyn.py",
line 9, in ?
    apply(bt.config, (), optionDict)
  File "D:\PYTHON\ACTIVESTATE\PYTHON21\lib\lib-tk\Tkinter.py", line
1085, in configure
    self.tk.call((self._w, 'configure')
TclError: unknown option "-labelText"


although bt is an intstance of LabelEntry and it has the labelText
option.

I think the problem is that I haven't defined a config method in my
Class LabelEntry. It thought that this is not necessary.

When I define a config method, I have the problem that some of the
LabelEntry options (padx and pady) are options for the packer of the
widgets inside (that is, Label and Entry). So, I can't just write
something like

    def config(self, kw):
        self.label.config(text=kw.get('labelText',''))
        self.entry.config(padx=kw.get('padx',4),
pady=kw.get('pady',4))

Puh, wired problem for me :<

I think I won't use apply, but use the call by dictionary option
instead.

Johannes



On Wed, 6 Feb 2002 11:18:18 +0000, Eric Brunel
<eric.brunel at pragmadev.com> wrote:

><posted & mailed>
>
>Hi Johannes,
>
>> def __init__(self, master=None, **kw):
>>       Frame.__init__(self, master)
>>      ...
>>       self.label = Label(self, text=kw['labelText'])
>> ...
>> 
>> But then, how do I handle default values, and how do I ensure that the
>> objects are created with the right parameters?
>
>Do not use kw['labelText'], but kw.get('labelText', ''). The get method on 
>a dictionary does just what you want. Please refer to:
>http://www.python.org/doc/current/lib/typesmapping.html
>
>HTH
> - eric -




More information about the Python-list mailing list