The tkinter
module allows to build graphical user interface.
Most of the time it is used with the ttk
module which extends and enhances tkinter's widgets.
The main advantages in using tkinter
, instead of other packages, are:
The following terms are used:
To get the tkinter
module install the package python3-tk
.
It will be installed in /usr/lib/python3.*/
.
To know which version of tkinter
is installed and if it has been properly installed, invoke Python with the option -m module-name
:
python3 -m tkinter
Since tkinter
is the Python transcription of Tk, the best way to learn how to use it is to understand how to map Tk into tkinter
.
Therefore, the way to use tkinter
is to match the Tk commands reference with the /tkinter/__init__.py
and beaar in mind that, to make a widget in Tk, the command is a list of tokens separated by space of the form:
classCommand pathName options
where:
classCommand
is the command that call the kind of widget to use;pathName
is a dot separated parent widget name starting from the root widget, indicated with .
, and are of the form .parent.child
;options
configure the widget's appearance and are of the form -opt val
.
The basic structure of a script that create a GUI with tkinter
and its expansion ttk
is:
import tkinter as tk import tkinter.ttk as ttk class Gui(tk.Frame): """GUI for the script.""" def __init__(self, master): """Initialize the interface.""" tk.Frame.__init__(self) def main(): """Run the GUI.""" root = tk.Tk() app = Gui(master=root) app.mainloop() if __name__ == "__main__": main()
tkinter
.