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 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.