O'Reilly logo

Tkinter GUI Application Development Blueprints - Second Edition by Bhaskar Chaudhary

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

Optimizing variables

The way you select variables in your program can considerably affect the speed of the execution of your program. For instance, if you do not need to change the content or attributes of a widget after its instantiation, do not create a class-wide instance of the widget.

For example, if a Label widget needs to remain static, use Label(root, text='Name').pack(side=LEFT) instead of using the following snippet:

self.mylabel = Label(root, text='Name')self.mylabel.pack(side=LEFT)

Similarly, do not create local variables if you are not going to use them more than once. For example, use mylabel.config (text= event.keysym) instead of first creating a local variable named key and then using it only once:

key = event.keysymmylabel.config ...

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