I am trying to implement a way of an entry only accepting integers being typed into it but I am struggling to get the code to work as desired. As I understand:
validate
which chooses when the validation should take place.validatecommand
decides how the data should be restrictedvalidatecommand
However, after attempting this I got the issue that it only the validates the string in the entry at the before even clicking on it.
from tkinter import*
root = Tk()
def equationpage(root):
vcmd = root.register(validate)
A_str = IntVar()
A_str.set(1)
a = Entry(root,width="2",textvariable=A_str,validate = "key",validatecommand = (vcmd,'%p'))
a.place(x=0,y=0)
def validate(inp):
try:
float(inp)
except:
return False
return True
equationpage(root)
mainloop()
With the code above no validation takes place and it lets me type anything into the entry. If I change the code above so that there is no default value for the entry I end up not being able to type anything into the entry as it keeps rejecting the input.
How can I solve this problem?
Based on this answer, there's no defined Tcl variable: '%p'
for this option.
Replace:
'%p'
to include the uppercase P instead:
'%P'