I understand that itโs already quite late for an answer, but I feel that I can give a much simpler answer to this ... it is really quite simple when you understand how it works.
Use the validation feature that comes with the Entry widget.
Suppose self is a widget:
vcmd = (self.register(self.callback)) w = Entry(self, validate='all', validatecommand=(vcmd, '%P')) w.pack() def callback(self, P): if str.isdigit(P) or P == "": return True else: return False
You do not need to include all replacement codes: ( '%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W' ), only those that you will use are needed.
The Entry widget returns a string, so you need to somehow extract any numbers to separate them from other characters. The easiest way to do this is to use str.isdigit() . This is a handy little tool that is built directly into the Python libraries and does not require additional import, and it identifies any digits (digits) found in the string returned by the Entry widget.
The or P == "" the or P == "" if statement allows you to delete your entire record, without it you cannot delete the last (first in the input field) digit because '%P' returns an empty value and calling your callback to return False . I will not go into details why here.
validate='all' allows focusin focusout evaluate the value of P when focusin , focusout or any key focusout contents of the widget, and therefore, you do not leave any holes for erroneous input of random characters.
In general, to make everything simple. If your callback returns True this will allow you to enter data. If the callback returns โFalse,โ it essentially โignoresโ keyboard input.
Check out these two links. They explain what each replacement code means and how to implement it.
http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/entry-validation.html http://stupidpythonideas.blogspot.ca/2013/12/tkinter-validation.html
EDIT: This will only take care of what is allowed in the box. However, inside the callback, you can add any P value to any variable of your choice.