A range-limited Spinbox widget

A number entry seems like it shouldn't be too complicated to deal with, but there are a number of subtleties to work through to make it bulletproof. In addition to limiting the field to valid number values, you'll want to enforce the from, to, and increment arguments as the minimum, maximum, and precision of the input, respectively.

The algorithm needs to implement the following rules:

  • Deletion is always allowed
  • Digits are always allowed
  • If from is less than 0, a minus is allowed as the first character
  • If increment has a decimal component, one dot is allowed
  • If the proposed value is greater than the to value, ignore the keystroke
  • If the proposed value requires more precision than increment, ignore the keystroke ...

Get Python GUI Programming with Tkinter now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.