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 ...

Get Tkinter GUI Application Development Blueprints - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.