Tkinter/Pmw and focus events

Greg McFarlane gregm at iname.com
Sun Jan 23 06:45:44 EST 2000


On 20 Jan, Timothy Grant wrote:
> Back to my other pet subject at the moment, Tkinter. I've been using Pmw
> and having really good success with it, especially since I develop on
> Linux and my clients run on Win98! Pmw allows one to assign the
> attribute 'modifiedcommand' to many of its widgets. That function is
> called any time the widget is modified. Is there a similar attribute for
> got-focus and lost-focus events?
> 
> I'd like to implement something like is in Delphi/Paradox where when the
> cursor is outside the field the numbers are formatted all pretty
> ($1,234.56), and when the cursor is inside the field it is automatically
> reformatted to a 'normal' number (1234.56).

If you are using Pmw.EntryField, then you should use the bind method
of the entry component.  You could bind to '<FocusIn>' and
'<FocusOut>' (or maybe to '<Enter>' and '<Leave>'.  For example, this
changes the test to lowercase when the entry widget has focus and to
uppercase when it loses focus:

import string
import Pmw
 
def focus_in(event = None):
    entryfield.setentry(string.lower(entryfield.get()))

def focus_out(event = None): 
    entryfield.setentry(string.upper(entryfield.get()))

root = Pmw.initialise()
 
entryfield = Pmw.EntryField() 
entryfield.pack() 

entryfield.component('entry').bind('<FocusIn>', focus_in) 
entryfield.component('entry').bind('<FocusOut>', focus_out) 
 
root.mainloop()

-- 
Greg McFarlane     INMS Telstra Australia     gregm at iname.com




More information about the Python-list mailing list