As programmers, we hardly need to understand the class hierarchy of Tkinter. After all, we have been able to code all the applications so far without bothering about the overall class hierarchy.
However, knowing about class hierarchy enables us to trace the origin of a method within the source code or source documentation of a method. A brief review of the class hierarchy will also help us prevent accidental overriding of methods in our programs.
In order to understand the class hierarchy of Tkinter, let us take a look at the source code of Tkinter. On Windows installation, the source code of Tkinter is located at C:Python27Liblib-tkTkinter.py
.
When we open this file in a code editor and look at its list of class definitions, we can see the following structure:
So, what do we notice here? We have class definitions for each of the core Tkinter widgets. In addition, we have class definitions for different geometry managers and different variable types defined within Tkinter. These class definitions are what you would normally expect to be there.
However, in addition to these, we notice some strange-looking class names, such as BaseWidget, Misc, Tk,
Toplevel, Widget
, and Wm
. All these classes are circled in the above screenshot. So what services do these classes provide, and where do they fit in the larger scheme of things?
Let's use the inspect
module to look at the class hierarchy of Tkinter. We will first inspect the class hierarchy of the Frame widget as a representation of class hierarchies for all other widgets. We will also look at the class hierarchy of the Tk
and Toplevel
classes to estimate their role in the overall class hierarchy of Tkinter (refer to 8.12 tkinter class hierarchy.py
):
import Tkinter import inspect print 'Class Hierarchy for Frame Widget' for i, classname in enumerate(inspect.getmro(Tkinter.Frame)): print'%s: %s'%(i, classname) print 'Class Hierarchy for Toplevel' for i, classname in enumerate(inspect.getmro(Tkinter.Toplevel)): print '%s: %s'%(i, classname) print 'Class Hierarchy for Tk' for i, classname in enumerate(inspect.getmro(Tkinter.Tk)): print'%s: %s'%(i, classname)
The output of the preceding program is as follows:
Class Hierarchy for Frame Widget 0: Tkinter.Frame 1: Tkinter.Widget 2: Tkinter.BaseWidget 3: Tkinter.Misc 4: Tkinter.Pack 5: Tkinter.Place 6: Tkinter.Grid Class Hierarchy for Toplevel 0: Tkinter.Toplevel 1: Tkinter.BaseWidget 2: Tkinter.Misc 3: Tkinter.Wm Class Hierarchy for Tk 0: Tkinter.Tk 1: Tkinter.Misc 2: Tkinter.Wm
The description of the preceding code is as follows:
getmro(classname)
function from the inspect module returns a tuple, consisting of all the ancestors of classname
in the order specified by the Method Resolution Order (MRO). Method Resolution Order refers to the order in which base classes are searched when looking for a given method.Frame
class inherits from the Widget
class, which in turn inherits from the BaseWidget
class.Frame
class also inherits from the Misc
class, which is a generic mixin that provides a lot of functionality that we have used in our applications.Misc
class, run the following commands into your Python interactive shell:>>> import Tkinter >>> help(Tkinter.Misc)
Tk
and Toplevel
classes.Tk
class represents the Toplevel widget of Tkinter, which represents the main window of an application. The Toplevel
class provides several methods for constructing and managing a Toplevel widget with a given parent.Toplevel
and Tk
classes, run the following commands into your Python interactive shell:>>>help(Tkinter.Toplevel) >>>help(Tkinter.Tk)
Misc
mixin class, the Toplevel
and Tk
classes also inherit methods from the Wm
mixin class.Wm
(window manager) mixin class provides a lot of functions to communicate with the window manager. For a list of functions provided by the Wm
class, run the following command into your Python interactive shell:>>>help(Tkinter.Wm)
After translating the class hierarchy—as obtained from the previous program—into an image, we get a hierarchy image similar to the one shown in the following:
In addition to the normal inheritance relation (shown in the preceding diagram by unspotted lines), Tkinter provides a list of mixins (or helper classes). A mixin is a class that is designed not to be used directly, but to be combined with other classes using multiple inheritances.
Tkinter mixins can be broadly classified into two categories:
Misc
class, which is used by the root window and widget classes, provides several Tk and window-related servicesWm
class, which is used by the root window and the Toplevel widget, provides several window manager services.18.222.164.141