When to use Variable classes? (BooleanVar, DoubleVar, IntVar, StringVar)

In a tkinter application, StringVar (as well as IntVar, BooleanVar, and DoubleVar) are very rarely needed. The underlying tcl/tk interpreter provides special features for all of its variables, so these wrappers exist to take advantage of those features.

The two big advantages that these variables have are:

  1. You can associate one variable with more than one widget, so that two or more widgets display exactly the same information all the time
  2. You can bind functions to be called when the values change.

My opinion is that you should not use them unless you specifically need one of those two features. If you just need to get or set the value of a widget there are methods to do that on the widget itself (eg: entry_widget.insert(…), label_widget.configure(text=”…”), etc).

I feel that they add overhead by introducing an additional object that needs to be managed, without providing any extra benefit unless you’re taking advantage of the two features described above.

Leave a Comment