What is Tkinter used for and how to install it?
Tkinter is the de facto way in Python to create Graphical User interfaces (GUIs) and is included in all standard Python Distributions. In fact, it’s the only framework built into the Python standard library.
This Python framework provides an interface to the Tk toolkit and works as a thin object-oriented layer on top of Tk. The Tk toolkit is a cross-platform collection of ‘graphical control elements’, aka widgets, for building application interfaces.
If you want to take advantage of the latest version of Tkinter, you’ll need to install a version of Python that supports Tcl/Tk 8.5 or greater. This will provide you with the Ttk (Tile extension integrated into Tk), which is required in order to run the current Tk widget set.
Warning: If you’re working on a system that comes with Python pre-installed, you may need to upgrade it to at least Python 3.7, which includes everything you need to run Tk out of the box.
To verify the Tcl version on your computer, enter:
$ tclsh % info patchlevel 8.6.8
To open a Tcl/Tk shell and verify that it is working, enter a command similar to the following:
On a Linux system:
On a Windows system:
A popup window titled wish8.6 will appear.
To verify that Tkinter is properly installed, enter:
$ python -m tkinter
A popup window that states the Tcl/tk version should appear.
What is Tkinter is Used For
This framework provides Python users with a simple way to create GUI elements using the widgets found in the Tk toolkit. Tk widgets can be used to construct buttons, menus, data fields, etc. in a Python application. Once created, these graphical elements can be associated with or interact with features, functionality, methods, data or even other widgets.
For example, a button widget can accept mouse clicks, and can also be programmed to perform some kind of action, such as exiting the application.
How to Use Tkinter
In the following example, Tkinter creates a Tk application frame and a button widget that exits the application when clicked.
from tkinter import * tk = Tk() frame = Frame(tk, borderwidth=2) frame.pack(fill=BOTH, expand=1) label = Label(frame, text="Button Example") label.pack(fill=X, expand=1) button = Button(frame, text="Exit", command=tk.destroy) button.pack(side=BOTTOM) tk.mainloop()
How to Download Tkinter for Python 3
As previously stated, the best way to get the latest version of Tkinter is to install Python 3.7 or later. But Tkinter can also be downloaded and installed as part of any standard Python 3 installation.
How to Install Tkinter
If you are using a Python 3 version prior to Python 3.7, or a non-standard version of Python 3 that does not include Tkinter, do the following:
On a Linux System:
$ sudo apt-get install python3-tk
On a Windows or Mac System:
The simplest and easiest method to install Tkinter, is to either: