problems subclassing a PMW widget

Greg Goodman chironsw at swbell.net
Tue Dec 11 18:24:14 EST 2001


I'm having a problem subclassing a Pmw widget, and I'm looking for some
help.  (I looked through the comp.lang.python archive, and found that
this question was asked last August, but apparently never answered.)

A simple code snippet that illustrates the problem follows:

--- start snippet ---

import sys
import Tkinter
import Pmw

class UOMEntryField(Pmw.EntryField):
   """
   A Pmw.EntryField subclass
   """

   def __init__(self, parent = None, **kw):
      Pmw.EntryField.__init__(self, parent, **kw)

if __name__ == '__main__':

   root = Tkinter.Tk()

   val = Tkinter.DoubleVar()
   val.set(1.23)

   widget = UOMEntryField(
         labelpos='w',
         label_text='My Label',
         validate='real',
         entry_textvariable = val)

   widget.pack()
   Tkinter.Button(text='Quit', command=sys.exit).pack()

   root.mainloop()

--- end snippet ---

The symptom is that the specified 'real' validation doesn't work.  If
'widget' is created as a Pmw.EntryField, everything works.  If it is a
UOMEntryField (as in the example above), it doesn't.  On any keyboard
entry into the field, I get a slew of error messages culminating in:

AttributeError: UOMEntryField instance has no attribute '_valid'

I have traced the problem to the Pmw.MegaArchetype.initialiseoptions()
method.  The relevant EntryField options are only initialised (and the
_entry attribute, among others, created) "if self.__class__ is
myClass:".  myClass is passed by the Pmw.EntryField constructor as
"EntryField".  Unfortunately, the class of self is UOMEntryField.

Either I'm doing something wrong, or the Pmw widgets aren't meant to be
subclassed.  I'm hoping it's me.

Incidentally, the reason I want to subclass the EntryField is to add a
"units of measure" MenuOption to the widget.  That way, when I create an
entry field, I can also specify a type of UOM, such as 'length' or
'density' or 'velocity', which produces a Menu of allowable units.  (For
instance, 'length' would imply 'feet', 'inches, 'meters', 'centimeters',
etc.)  I could also specify an initial unit, such as 'feet' or
'grams/liter' (density) or 'meters/second' (velocity).  On changing the
UOM selection with the MenuOption, the current value of the field gets
automatically converted.

Looking forward to any suggestions you can give me,

Greg Goodman
chironsw.NOSPAM at swbell.net



More information about the Python-list mailing list