tkinter: the SaYa idiom

Posted on Sun 19 March 2017 in Software Engineering

Background

This morning I landed on this question asked on StackOverflow where the OP stumbled on this error he shared through his/her comment to my answer:

NoneType' object has no attribute 'destroy'

The origin of the problem is that the OP created a button widget within the class initializer and positioned in the same time:

self.widget_object = ttk.WidgetType(arguments).pack()

When a mouse click event is performed, the callback is executed:

self.widget_object.destroy()

Explanation

The Python interpreter raises a TypeError exception late at runtime because it was assigned to destroy the None singleton object.

In Python, everything is an object (yes, including classes), to achieve this principle, Python forces functions without a return type to return the None object. This is the case of the 3 methods used to place widgets in tkinter: place(), pack() and grid().

This means the value of self.widget_object in the initializer is None. No wonder the interpreter can not apply destroy() method on it.

Motivation

Similar situations are frequent when developing GUIs in tkinter among newbies as well as experienced developers. That is why I created the SaYa idiom which I name using my parents's first names first syllables to thank them for giving me the opportunity to live and a chance they were not able to afford for themselves: to study.

Statement

SaYa idiom states: When building a GUI in tkinter, always place widgets after their creation.

Advantages

An idiom has at least one reason to exist. Here are the benefits of SaYa on tkinter GUIs:

  • Scalability
  • Ease of maintenance
  • Narrowing down the attack surface on applications as this reduces bugs

SaYa is an idiom that proves to be benefical for all Python-tkinter developers.