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

Johannes Eble skywalkerpackage at hotmail.com
Wed Feb 6 04:46:46 EST 2002


Hello Laura,

thanks again for your explanations.

Now I have another problem:

I want to use my own widgets in the same scheme. For example, let's
say I have created the widget

LabelEntry

which just combines a label and an entry side by side in a row. The
widget inherits from Frame:

class LabelEntry(Frame):
    """Valid options: labelText, padx, pady"""
    def __init__(self, master=None, labelText='', padx=4, pady=4):
        Frame.__init__(self, master)
        self.label = Label(self, text=labelText)
        self.entry = Entry(self)
        self.label.pack(side=TOP, anchor=W)
        self.entry.pack(side=TOP, anchor=W, padx=padx, pady=pady)

This doesn't work together with an optionDict={'labelText':'hello',
'padx':'6'}

and

wt=LabelEntry(...)
apply(wt.config, (), optionDict)

I get the following 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\pane2c.py", line 42,
in ?
    apply(wt.config, (), dictOptions)
  File "D:\PYTHON\ACTIVESTATE\PYTHON21\lib\lib-tk\Tkinter.py", line
1085, in configure
    self.tk.call((self._w, 'configure')
TclError: unknown option "-labelText"

I guess the problem is that Python forwards the optionDict to Frame
and can't use them there.

I have also tried to understand how it is done in e.g. the Button
widget which inherits from Widget which inherits from BaseWidget
(a.a.), but I don't understand what's going on in Tkinter.py. Also, I
have tried to write the constructor of LabelEntry sort of

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? Maybe I should use a
local dictionary

self.defaultOptionsLabel={'labelText':''}
self.defaultOptionsEntry={...}

then test if a key is in the optionDict parameter and, if yes, then
overwrite the dictionarys above, accordingly and use them to configure
the basic widgets.

But this seems rather cumbersome to me.


Is there an elegant solution to this problem?


ByeBye

Johannes






More information about the Python-list mailing list