Validation in Tkinter

Tkinter's validation system is one of those parts of the toolkit that is less than intuitive. It relies on the following three configuration options that we can pass into any input widget:

  • validate: This option determines which type of event will trigger the validation callback
  • validatecommand: This option takes the command that will determine if the data is valid
  • invalidcommand: This option takes a command that will run if validatecommand returns False

This seems pretty straightforward, but there are some unexpected curves.

The values we can pass to validate are as follows:

Validates string Triggers when
none It is none that turns off validation
focusin The user enters or selects the widget
unfocus The user ...

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.