User Tools

Site Tools


programming-languages:tkinter:start

tkinter

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:

  • easy to learn;
  • easy to create graphical user interfaces;
  • mature and under continuous, active development.

Terminology

The following terms are used:

  • Tcl: is a dynamic programming language.
  • Tk is the standard GUI toolkit for Tcl and for many other dynamic languages and can produce native applications that run unchanged across different operating systems;
  • tkinter is the standard Python GUI to the Tk GUI toolkit.

Installation

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

Mapping Tk into 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.

Basic structure of a GUI script

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()
  • tcl.tk/man/: Tcl/Tk's official documentation (refer to the last version of the Tk commands link)
  • tkdocs.com/tutorial/: tutorial about Tcl/Tk and the equivalent map into tkinter.
programming-languages/tkinter/start.txt · Last modified: 2020/04/03 09:36 (external edit)