Creating a DateEntry widget

Let's try creating a validating version of our Date field. We'll make a DateEntry widget that prevents most erroneous keystrokes, then checks for date validity on focusout. If the date is invalid, we'll mark the field in some way and display an error. Let's perform the following steps to do the same:

  1. Open a new file called and begin with the following code:
from datetime import datetime

class DateEntry(ttk.Entry):
    """An Entry for ISO-style dates (Year-month-day)"""

    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)
            validatecommand=(                self.register(self._validate),                '%S', '%i', '%V', '%d' ), invalidcommand=(self.register(self._on_invalid), ...

Get Python GUI Programming - A Complete Reference Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.