Book Image

Python GUI programming with Tkinter

By : Alan D. Moore
Book Image

Python GUI programming with Tkinter

By: Alan D. Moore

Overview of this book

Tkinter is a lightweight, portable, and easy-to-use graphical toolkit available in the Python Standard Library, widely used to build Python GUIs due to its simplicity and availability. This book teaches you to design and build graphical user interfaces that are functional, appealing, and user-friendly using the powerful combination of Python and Tkinter. After being introduced to Tkinter, you will be guided step-by-step through the application development process. Over the course of the book, your application will evolve from a simple data-entry form to a complex data management and visualization tool while maintaining a clean and robust design. In addition to building the GUI, you'll learn how to connect to external databases and network resources, test your code to avoid errors, and maximize performance using asynchronous programming. You'll make the most of Tkinter's cross-platform availability by learning how to maintain compatibility, mimic platform-native look and feel, and build executables for deployment across popular computing platforms. By the end of this book, you will have the skills and confidence to design and build powerful high-end GUI applications to solve real-world problems.
Table of Contents (23 chapters)
Title Page
Copyright and Credits
Dedication
Packt Upsell
Contributors
Preface
Index

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 leaves the widget

focus

Either focusin or focusout

key

The user enters text in the widget

all

focusin, focusout, and key

 

The validatecommand argument is where things get tricky. You might think this takes the name of a Python function or method, but that's not quite it. Instead, we...