<KeyPress>, focus, and Pmw widgtes

Chad Netzer cnetzer at mail.arc.nasa.gov
Thu Apr 3 19:46:52 EST 2003


On Thu, 2003-04-03 at 16:02, Marc wrote:
> Hi,
> 
> I was trying to use a keypress to trigger an event in a Gui. Reading
> through past postings I discovered that the widget has to have focus
> to actually register the event. However, even setting the focus on the
> widget component I am unable to trigger the event. Obviously doing
> something wrong, I look to the masses for help. Here is my code:

First of all, in the future, please post a working example, complete
with import statements, class names, mainloop() etc., so that I or
others do not have to spend our time typing it all in.  You will have
better luck with responses, I'd wager.

Next, the event type you are looking for is '<KeyRelease>', not
'<KeyPress-Up>'.

Third, the Pmw widget probably intercepts (or at least, doesn't forward)
these event types.  You can try the following:

instead of:

    self.fps.bind( '<KeyPress-Up>', self.getTBPS)

do this:

    self.fps.component('entry').bind( '<KeyRelease>', self.getTBPS)

This sets the binding on the actual entry Tkinter component of the Pmw
mega widget.  This will get you started.  However, it is perhaps not the
best way to do what you want.

For one thing, it may possibly interfere with the operations of the
EntryField widget as a whole (I can't remember if new bindings override
old ones).  But mainly, it won't be triggerred for key-repeat events,
which you may actually want.

Instead, you should read about the validator options of an EntryField
widget.  This can be made to call a function whenever the contents of
the entry widget are modified (where by keypress, or cut-n-paste, etc.)

So, instead of "validate=None", when you create the widget, try
"validate=self.getTBPS".  You will need to return a True or False value
to indicate whether the validation actually worked.  You will also need
to change other things, but that will get you started.  Use the
facilities that Pmw provides; the author spent a lot of time making them
all work reasonably well.


-- 
Bay Area Python Interest Group - http://www.baypiggies.net/

Chad Netzer
(any opinion expressed is my own and not NASA's or my employer's)







More information about the Python-list mailing list