Skip to Content
Python GUI Programming with Tkinter
book

Python GUI Programming with Tkinter

by Alan D. Moore
May 2018
Beginner to intermediate content levelBeginner to intermediate
452 pages
11h 26m
English
Packt Publishing
Content preview from Python GUI Programming with Tkinter

A Date widget

Now, let's apply the mixin class to the DateEntry class we made before, keeping the same validation algorithm as follows:

class DateEntry(ValidatedMixin, ttk.Entry):    def _key_validate(self, action, index, char, **kwargs):        valid = True        if action == '0':            valid = True        elif index in ('0', '1', '2', '3', '5', '6', '8', '9'):            valid = char.isdigit()        elif index in ('4', '7'):            valid = char == '-'        else:            valid = False        return valid    def _focusout_validate(self, event):        valid = True        if not self.get():            self.error.set('A value is required')            valid = False        try:            datetime.strptime(self.get(), '%Y-%m-%d')        except ValueError:            self.error.set('Invalid date')            valid = False        return valid

Again, pretty simple, all we need to do is specify the validation ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python GUI Programming with Tkinter - Second Edition

Python GUI Programming with Tkinter - Second Edition

Alan D. Moore

Publisher Resources

ISBN: 9781788835886Supplemental Content