I am building an app that requires people to put numbers into entry boxes, I am using tkinter, on IDLE 3, and I was wondering if there is a way to make it so people can't put letters into the entry boxes? I want numbers to be the only thing able to be put into the text boxes.
Asked
Active
Viewed 54 times
0
-
If tkinter is anything like wxpython, there should be an event whenever the user edits the box. Make that event handler check if there are any invalid characters – wnnmaw May 08 '14 at 19:00
1 Answers
-1
The best way is, you can use try/except
method. Try to convert user input using int
, if no exception raised, then you're good to go (as you want only numbers). On the other hand if an exception is raised, do something else:
# suppose the input is stored in the variable 'user_input'
try:
int(user_input)
except ValueError:
# do something else

Ahmed Sadman
- 105
- 10