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

Johannes Eble skywalkerpackage at
Wed Feb 6 10:46:46 CET 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


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',


apply(wt.config, (), optionDict)

I get the following error message:

Traceback (most recent call last):
line 301, in RunScript
    exec codeObject in __main__.__dict__
  File "D:\python\activestate\python21\myscripts\", line 42,
in ?
    apply(wt.config, (), dictOptions)
  File "D:\PYTHON\ACTIVESTATE\PYTHON21\lib\lib-tk\", line
1085, in configure, '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 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


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?



More information about the Python-list mailing list