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

Dynamically adjusting the Spinbox range

Our ValidatedSpinbox method seems adequate for most of our fields. But consider the Height fields for a moment. It would not make any sense for the Mini height value to be more than the Max height value, or for the Median height value not to be between them. Is there some way we can work this kind of interdependent behavior into our class?

We can! To do this, we'll rely on the tracing feature of Tkinter variables. A trace is essentially a hook into the .get() and .set() methods of variables that allows you to trigger any Python function or method when a variable is read or changed.

The syntax is as follows:

sv = tk.StringVar()
sv.trace('w', some_function_or_method)
The first argument to .trace() indicates ...

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