O'Reilly logo

Python GUI Programming with Tkinter by Alan D. Moore

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 DateEntry.py 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)
        self.config(
            validate='all',
            validatecommand=(                self.register(self._validate),                '%S', '%i', '%V', '%d' ), invalidcommand=(self.register(self._on_invalid), ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required