Validating Entry in tkinter

Peter Otten __peter__ at web.de
Mon Jul 25 15:08:08 EDT 2011


Terry Reedy wrote:

> On 7/25/2011 8:31 AM, Peter Otten wrote:
>> Saul Spatz wrote:
>>
>>> That doesn't work, I'm being stupid,  The user might type anywhere in
>>> the
>>> string, not just at the end.  I need
>>>
>>> return all([c in '1234567890abcdefABCDEF ' for c in after])
> 
> If one wants to validate keystrokes, rather than the entire field after
> the fact, 

It's not really after the fact as the user will not see the new contents 
unless they are accepted by the validatecommand handler.

> is it possible to set an onkey handler, that will pass on
> valid keys?

With validatecommand you can have tkinter provide the string that is being 
inserted:

import tkinter as tk

MESSAGE = ("about to insert {text!r} at position {index} "
           "({resolution})")

def validate(action, index, text):
    if action == "1":
        accept = text.isdigit()
        print(
            MESSAGE.format(
                resolution="OK" if accept else "rejected",
                text=text,
                index=index))
        return accept
    return True

if __name__ == "__main__":
    root = tk.Tk()
    cmd = (root.register(validate), "%d", "%i", "%S")
    entry = tk.Entry(root, validate="all", validatecommand=cmd)
    entry.pack()
    entry.focus_set()
    root.mainloop()

The available format codes are listed at 
http://www.tcl.tk/man/tcl8.4/TkCmd/entry.htm#M16

If you need something more specific you'd probably have to bind the 
<KeyPress> event to a custom handler:

import tkinter as tk

def keypress(event):
    print(event.char)
    if event.char:
        if not event.char.isdigit() and event.char != "\b":
            return "break"
    else:
        print("Don't know what to do with key #", event.keycode)

if __name__ == "__main__":
    root = tk.Tk()
    entry = tk.Entry(root)
    entry.bind("<KeyPress>", keypress)
    entry.pack()
    entry.focus_set()
    root.mainloop()





More information about the Python-list mailing list