0

i am getting my user to enter a value in an entry box and then a "key" validation checks if the number is okay. however this check occurs before the new value registers so it is 1 out of date

example:

from tkinter import *
en_first = Entry(window,validatecommand=first_check,width=5,validate='key')
def first_check():
if en_first.get() == '':
    print("no value")
    return True
elif en_first.get() == '0':
    print("number is 0")
    return True
elif en_first.get() < '9':
    print("to small")
    return True
elif en_first.get() > '9':
    print("big enough")
    return True
  • What is your expected behaviour and what rule do you wish to apply to the validation? – scotty3785 Nov 03 '21 at 13:21
  • That's because the validation takes place before an edit is accepted. The correct way to validate is shown [here](https://stackoverflow.com/questions/4140437/interactively-validating-entry-widget-content-in-tkinter). You can use %d to check if you're dealing with an insertion, then %S to check if the new char is a digit and then %P to see if the to-be-accepted number satisfies your conditions. – Reti43 Nov 03 '21 at 13:45

0 Answers0