The class hierarchy of Tkinter

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 with the overall class hierarchy. However, knowing the class hierarchy enables us to trace the origin of a method within the source code or source documentation of a method.

In order to understand the class hierarchy of Tkinter, let's take a look at the source code of Tkinter. On the Windows installation, the source code of Tkinter is located at pathofPythonInstallationLib kinter. On my Linux machine, the
source code is located at /usr/lib/python3.6/tkinter/ .

If you open the __init__.py file from this folder in a code editor and look at its list of class definitions in Tkinter, you will see the following structure:

So, what can you see here? We have class definitions for each core Tkinter widget.

In addition to this, we have class definitions for different geometry managers and variable types defined within Tkinter. These class definitions are what you would normally expect to be there.

However, in addition to these, you will see some more class names, such as BaseWidget, Misc, Tk, Toplevel, Widget, and Wm. All of these classes are circled in the preceding 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 the 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 (10.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( ' {}: {}'.format(i, classname))

print ('Class Hierarchy for Toplevel')
for i, classname in enumerate(inspect.getmro(tkinter.Toplevel)):
print (' {}:{}'.format(i, classname))

print ('Class Hierarchy for Tk')
for i, classname in enumerate(inspect.getmro(tkinter.Tk)):
print (' {}: {}'.format(i, classname))

The output of the preceding program is as follows:

Class Hierarchy for Frame Widget
0: <class 'tkinter.Frame'>
1: <class 'tkinter.Widget'>
2: <class 'tkinter.BaseWidget'>
3: <class 'tkinter.Misc'>
4: <class 'tkinter.Pack'>
5: <class 'tkinter.Place'>
6: <class 'tkinter.Grid'>
7: <class 'object'>
Class Hierarchy for Toplevel
0:<class 'tkinter.Toplevel'>
1:<class 'tkinter.BaseWidget'>
2:<class 'tkinter.Misc'>
3:<class 'tkinter.Wm'>
4:<class 'object'>
Class Hierarchy for Tk
0: <class 'tkinter.Tk'>
1: <class 'tkinter.Misc'>
2: <class 'tkinter.Wm'>
3: <class 'object'>

The description of the preceding code is as follows:

  • The 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). MRO refers to the order in which base
    classes are searched when looking for a given method.
  • By inspecting the MRO and the source code, you will come to know that the Frame class inherits from the Widget class, which in turn inherits from the BaseWidget class. 
  • The Widget class is an empty class with the following class definition: class Widget(BaseWidget, Pack, Place, Grid).
  • As you can see, this is how methods defined in the geometry manager (the pack, place, and grid mix-ins) are made available to all the widgets.
  • The BaseWidget class has the following class definition: class BaseWidget(Misc). This class exposes the destroy method that can be used by programmers. 
  • All the utility methods defined in the Misc class are made available to the widgets at this hierarchy level.
  • The Misc class is a generic mix-in that provides a lot of functionality that we have used in our applications. Some of the methods that we have used in our programs, as defined within the Misc class, are after(), bbox(), bind_all()bind_tag(), focus_set(), mainloop(), update(), update_idletask(), and winfo_children(). For a complete list of functionalities provided by the Misc class, run the following commands in the Python interactive shell:
>>> import tkinter
>>> help(tkinter.Misc)

Now, let's take a look at the Tk and Toplevel classes:

  • The Tk class returns a new Toplevel widget on the screen. The __init__ method of the Tk class is responsible for the creation of a new Tcl interpreter by calling a method named loadtk(). The class defines a method named report_callback_exception(), which is responsible for the reporting of errors and exceptions on sys.stderr.
  • The __init__ method of the Toplevel class of Tkinter is responsible for the creation of the main window of an application. The constructor of the class takes various optional arguments such as bg, background, bd, borderwidth, class, height, highlightbackground, highlightcolor, highlightthickness, menu, and relief.
  • To obtain a list of all the methods provided by the Toplevel and Tk classes, run the following command in the Python interactive shell:  help(tkinter.Toplevel); help(tkinter.Tk).
  • In addition to inheriting from the Misc mixin class, the Toplevel and Tk classes also inherit methods from the Wm mixin class.
  • The Wm (short for the Window manager) mixin class provides many methods to let us communicate with the window manager. Some commonly used methods from this class include wm_iconify, wm_deiconify, wm_overrideredirect, title, wm_withdraw, wm_transient, and wm_resizable. For a complete list of functions provided by the Wm class, run the following command in the Python interactive shell: help(tkinter.Wm).

After translating the class hierarchy, as obtained from the previous program and by inspecting the source code, we get a hierarchy structure of Tkinter, as shown in the following diagram:

In addition to the normal inheritance relation, which is shown in the preceding diagram with the help of unbroken 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 the following two categories:

  • Geometry mixins: These include the Grid, Pack, and Place classes
  • Implementation mixins: These include the following classes:
    • The Misc class, which is used by the root window and widget classes,  provides several Tk and window-related services
    • The Wm class, which is used by the root window and the Toplevel widget, provides several window manager services

This concludes our brief under-the-hood tour of Tkinter. Hopefully, this should give you some insight into the inner workings of Tkinter. If you ever have a doubt about the documentation of any given method, you can take a peek directly into the actual implementation of the method.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.50.222