In this article we will talk about the Tkinter toolkit which is simply a link for the Python programming language to the Tk graphics library allowing you to use all the tools made available by it. The Tkinter library is used for the creation of cross-platform GUI (Graphical User Interface), which we find already included in the installed Python distribution, even if it is a not very modern technology it still remains very popular among developers being one of the most used in the world, particularly suitable for those small projects that do not require large actions. Among the various advantages that it boasts it is possible to highlight its extreme simplicity of use, while still providing professional graphic interfaces with a high number of widgets at its disposal. As previously mentioned Tkinter is a multiplatform library that allows you to create applications for the various operating systems in use such as Windows, Linux and Mac, a task that is not always possible with other graphic toolkits. Note that the interfaces created with Tkinter even if they provide good looking widgets have a slightly different style from the styles of the environments they run on, even if they are very similar to them.
There are two ways to create layouts in Tkinter:
- Writing code manually, using any text editor such as pycharm, Atom, Visual studio code and many others ;
- Using a cross-platform graphics editor called page based on a drag-and-drop system .
Let’s now look at some small examples:
import Tkinter as tk """I import the library for the Python 3 version"""
With the code written above the Tkinter library was imported into the project, from this moment all its tools are made available. You have been given an alias to the name to speed up code writing. Note that the version for Python 3 differs from the version for Python 2 by the name of the library, which is Tkinter for version 3 and Tkinter for version 2, this detail points out that the two versions are not compatible with each other.
import Tkinter as tk """I import the library for the Python 2 version"""
In this way, Tkinter is imported into the Python 2 version. If we want to make a program that works in both circumstances we have to write the following code:
from sys import version_info
if version_info.major == 2:
"""We use version 2"""
import Tkinter as tk
elif version_info.major == 3:
"""We use version 3"""
import Tkinter as tk
Let’s see now how to create a simple window.
import Tkinter as tk """I import the library"""
window=tk.Tk() """I instantiate the window"""
window.geometry("600x400+800+300") """I define size and position"""
window.title("My window") """I define the title of the window"""
window.mainloop() """I create the window"""
In figure 1 the result of the previous listing.
Now we can insert some element like a label and a button.
import Tkinter as tk """I import the library"""
window=tk.Tk() """I instantiate the window"""
window.geometry("600x400+800+300") """I define size and position"""
window.title("My window") """I define the title of the window"""
"""I create a label"""
text = tk.Label(text="This is my beautiful text")
"""I create a button"""
bottone = tk.Button(text="start")
"""places them in the container"""
text.pack()
bottone.pack()
window.mainloop() """I create the window"""
In the code a Label element and a Button element have been installed, inserted in the window container through the pack () action which is one of the three methods of Tkinter to position the elements in the layout. In the following figure you can see the result. It should be noted that at the moment neither spaces, nor dimensions, nor styles have been applied to the widgets used, we will see later in the following articles how to do this.
As mentioned above there are three methods of layout management, which are pack, grid, and place.
Pack: Allows you to place widgets on vertical or horizontal lines in relation to each other.
Grid: The widgets are placed in a grid, the position is determined by the row and column number entered.
Place: The various elements are arranged through the position of two dimensions with absolute coordinates x and y.
Before closing we make a little mention of Ttk, which is an extension of the Tkinter library with the use of new aesthetically improved widgets, giving a more pleasant appearance on all platforms in use. However, the replacement widgets are not fully compatible. The main difference is that some options are no longer present in Ttk widgets.
In the next articles we will deepen “how a graphical interface is built” by expanding in more detail the various topics mentioned on this page.
SviluppoMania
StayTuned