Tkinter

Last updated
License Python license
Website wiki.python.org/moin/TkInter   OOjs UI icon edit-ltr-progressive.svg

Tkinter is a Python binding to the Tk GUI toolkit. It is the standard Python interface to the Tk GUI toolkit, [1] and is Python's de facto standard GUI. [2] Tkinter is included with standard Linux, Microsoft Windows and macOS installs of Python.

Contents

The name Tkinter comes from Tk interface. Tkinter was written by Steen Lumholt and Guido van Rossum, [3] then later revised by Fredrik Lundh. [4]

Tkinter is free software released under a Python license. [5]

Description

As with most other modern Tk bindings, Tkinter is implemented as a Python wrapper around a complete Tcl interpreter embedded in the Python interpreter. Tkinter calls are translated into Tcl commands, which are fed to this embedded interpreter, thus making it possible to mix Python and Tcl in a single application.

There are several popular GUI library alternatives available, such as Kivy, Pygame, Pyglet, PyGObject, PyQt, PySide, and wxPython.

Some definitions

Window

This term has different meanings in different contexts, but in general it refers to a rectangular area somewhere on the user's display screen.

Top-level window

A window which acts as a child of the primary window. It will be decorated with the standard frame and controls for the desktop manager. It can be moved around the desktop and can usually be resized.

Widget

The generic term for any of the building blocks that make up an application in a graphical user interface.

  • Core widgets: The containers: frame, labelframe, toplevel, paned window. The buttons: button, radiobutton, checkbutton (checkbox), and menubutton. The text widgets: label, message, text. The entry widgets: scale, scrollbar, listbox, slider, spinbox, entry (singleline), optionmenu, text (multiline), and canvas (vector and pixel graphics).
  • Tkinter provides three modules that allow pop-up dialogs to be displayed: tk.messagebox (confirmation, information, warning and error dialogs), tk.filedialog (single file, multiple file and directory selection dialogs) and tk.colorchooser (colour picker).
  • Python 2.7 and Python 3.1 incorporate the "themed Tk" ("ttk") functionality of Tk 8.5. [6] [7] This allows Tk widgets to be easily themed to look like the native desktop environment in which the application is running, thereby addressing a long-standing criticism of Tk (and hence of Tkinter). Some widgets are exclusive to ttk, such as the combobox, progressbar, treeview, notebook, separator and sizegrip. [8]

Frame

In Tkinter, the Frame widget is the basic unit of organization for complex layouts. A frame is a rectangular area that can contain other widgets.

Child and parent

When any widget is created, a parent–child relationship is created. For example, if you place a text label inside a frame, the frame is the parent of the label.

A minimal application

Here is a minimal Python 3 Tkinter application with one widget: [9]

#!/usr/bin/env python3fromtkinterimport*root=Tk()# Create the root (base) window w=Label(root,text="Hello, world!")# Create a label with wordsw.pack()# Put the label into the windowroot.mainloop()# Start the event loop

For Python 2, the only difference is the word "tkinter" in the import command will be capitalized to "Tkinter". [10]

Process

There are four stages to creating a widget [11]

Create
create it within a frame
Configure
change the widgets attributes.
Pack
pack it into position so it becomes visible. Developers also have the option to use .grid() (row=int, column=int to define rows and columns to position the widget, defaults to 0) and .place() (relx=int or decimal, rely=int or decimal, define coordinates in the frame, or window).
Bind
bind it to a function or event.

These are often compressed, and the order can vary.

Simple application

Using the object-oriented paradigm in Python, a simple program would be (requires Tcl version 8.6, which is not used by Python on MacOS by default):

#!/usr/bin/env python3importtkinterastkclassApplication(tk.Frame):def__init__(self,root=None):tk.Frame.__init__(self,root)self.grid()self.createWidgets()defcreateWidgets(self):self.medialLabel=tk.Label(self,text='Hello World')self.medialLabel.config(bg="#00ffff")self.medialLabel.grid()self.quitButton=tk.Button(self,text='Quit',command=self.quit)self.quitButton.grid()app=Application()app.root.title('Sample application')app.mainloop()

See also

IDLE -- example of real usage Python's IDLE.png
IDLE — example of real usage

Related Research Articles

<span class="mw-page-title-main">FLTK</span>

Fast Light Toolkit (FLTK) is a cross-platform widget library for graphical user interfaces (GUIs), developed by Bill Spitzak and others. Made to accommodate 3D graphics programming, it has an interface to OpenGL, but it is also suitable for general GUI programming.

<span class="mw-page-title-main">Graphical widget</span> Element of interaction in a graphical user interface

A graphical widget in a graphical user interface is an element of interaction, such as a button or a scroll bar. Controls are software components that a computer user interacts with through direct manipulation to read or edit information about an application. User interface libraries such as Windows Presentation Foundation, Qt, GTK, and Cocoa, contain a collection of controls and the logic to render these.

<span class="mw-page-title-main">PyQt</span> Python GUI library

PyQt is a Python binding of the cross-platform GUI toolkit Qt, implemented as a Python plug-in. PyQt is free software developed by the British firm Riverbank Computing. It is available under similar terms to Qt versions older than 4.5; this means a variety of licenses including GNU General Public License (GPL) and commercial license, but not the GNU Lesser General Public License (LGPL). PyQt supports Microsoft Windows as well as various kinds of UNIX, including Linux and MacOS.

<span class="mw-page-title-main">PyGTK</span> Set of Python wrappers for the GTK graphical user interface library

PyGTK is a set of Python wrappers for the GTK graphical user interface library. PyGTK is free software and licensed under the LGPL. It is analogous to PyQt/PySide and wxPython, the Python wrappers for Qt and wxWidgets, respectively. Its original author is GNOME developer James Henstridge. There are six people in the core development team, with various other people who have submitted patches and bug reports. PyGTK has been selected as the environment of choice for applications running on One Laptop Per Child systems.

<span class="mw-page-title-main">Fox toolkit</span>

The FOX toolkit is an open-source, cross-platform widget toolkit, i.e. a library of basic elements for building a graphical user interface (GUI). FOX stands for Free Objects for X.

wxPython Python wrapper for wxWidgets

wxPython is a wrapper for the cross-platform GUI API wxWidgets for the Python programming language. It is one of the alternatives to Tkinter. It is implemented as a Python extension module.

gtkmm is the official C++ interface for the popular GUI library GTK. gtkmm is free software distributed under the GNU Lesser General Public License (LGPL).

<span class="mw-page-title-main">Tree view</span>

A tree view is a graphical widget within a graphical user interface (GUI) in which users can navigate and interact intuitively with concise, hierarchical data presented as nodes in a tree-like format. It can also be called an outline view.

<span class="mw-page-title-main">Tkhtml</span>

Tkhtml is a discontinued open-source browser engine written in C using the Tk widget toolkit. It was used primarily by the Html Viewer 3 (Hv3) minimalist web browser.

<span class="mw-page-title-main">Tk (software)</span> GUI toolkit or framework

Tk is a cross-platform widget toolkit that provides a library of basic elements of GUI widgets for building a graphical user interface (GUI) in many programming languages. It is free and open-source software released under a BSD-style software license.

<span class="mw-page-title-main">Zenity</span> Free software that allows the execution of GTK dialog boxes in command-line and shell scripts

Zenity is free software and a cross-platform program that allows the execution of GTK dialog boxes in command-line and shell scripts.

<span class="mw-page-title-main">PySide</span>

PySide is a Python binding of the cross-platform GUI toolkit Qt developed by The Qt Company, as part of the Qt for Python project. It is one of the alternatives to the standard library package Tkinter. Like Qt, PySide is free software. PySide supports Linux/X11, macOS, and Microsoft Windows. The project can also be cross compiled to embedded systems like Raspberry Pi, and Android devices.

<span class="mw-page-title-main">Abstract Window Toolkit</span> Java-based GUI toolkit

The Abstract Window Toolkit (AWT) is Java's original platform-dependent windowing, graphics, and user-interface widget toolkit, preceding Swing. The AWT is part of the Java Foundation Classes (JFC) — the standard API for providing a graphical user interface (GUI) for a Java program. AWT is also the GUI toolkit for a number of Java ME profiles. For example, Connected Device Configuration profiles require Java runtimes on mobile telephones to support the Abstract Window Toolkit.

<span class="mw-page-title-main">Veusz</span> Plotting software

Veusz is a scientific plotting package. Veusz is a Qt application written in Python, PyQt and NumPy. It is freely available for anyone to distribute under the terms of the GPL. It is designed to produce publication-quality plots. The name should be pronounced as "views".

wxPHP is an extension the programming language PHP that wraps the wxWidgets library, which allows writing cross-platform software desktop applications that make use of the native graphical components available to the different platforms. It supports the three major operating systems: Windows, Linux, and macOS by using PHP. Applications are written in PHP, which is an interpreted language. Thus, it needs no intermediate compiling step to run an application, if the PHP interpreter has the extension available.

<span class="mw-page-title-main">Kivy (framework)</span> Free and multi-platform graphical library for Python

Kivy is a free and open source Python framework for developing mobile apps and other multitouch application software with a natural user interface (NUI). It is distributed under the terms of the MIT License, and can run on Android, iOS, Linux, macOS, and Windows.

Tcl is a high-level, general-purpose, interpreted, dynamic programming language. It was designed with the goal of being very simple but powerful. Tcl casts everything into the mold of a command, even programming constructs like variable assignment and procedure definition. Tcl supports multiple programming paradigms, including object-oriented, imperative, functional, and procedural styles.

appJar is a cross-platform Python library for developing GUIs. It can run on Linux, OS X, and Windows. It was conceived, and continues to be developed with educational use as its focus, so is accompanied by comprehensive documentation, as well as easy-to-follow lessons.

<span class="mw-page-title-main">Fyne (software)</span> Graphical toolkit for building cross platform GUIs

Fyne is a free and open-source cross-platform widget toolkit for creating graphical user interfaces (GUIs) across desktop and mobile platforms. It is designed to enable developers to build applications that run on multiple desktop and mobile platforms/versions from a single code base. Fyne uses OpenGL to provide cross-platform graphics. It is inspired by the principles of Material Design to create applications that look and behave consistently across all platforms. It is licensed under the terms of the 3-clause BSD License, supporting the creation of free and proprietary applications. In December 2019 Fyne became the most popular GUI toolkit for Go, by GitHub star count and in early February 2020 it was trending as #1 project in GitHub trending ranks.

References

  1. "Tkinter — Python interface to Tcl/Tk — Python v2.6.1 documentation" . Retrieved 2009-03-12.
  2. "Tkinter - Pythoninfo Wiki".
  3. tkinter—Python interface to Tcl/Tk—Python 3.9.10 Documentation
  4. Shipman, John W. (2010-12-12), Tkinter reference: a GUI for Python, New Mexico Tech Computer Center, retrieved 2012-01-11
  5. "Tkinter - Tkinter Wiki". Archived from the original on 2013-11-13. Retrieved 2013-11-13.
  6. "Python issue #2983, "Ttk support for Tkinter"".
  7. "Python subversion revision 69051, which resolves issue #2983 by adding the ttk module".
  8. "Tkinter ttk widgets - Python Tutorial". CodersLegacy. Retrieved 2022-01-13.
  9. "Tkinter 8.5 reference: a GUI for Python".
  10. Fleck, Dan. "Tkinter – GUIs in Python" (PDF). CS112. George Mason University. Retrieved 18 August 2018.
  11. Klein, Bernd. "GUI Programming with Python: Events and Binds". www.python-course.eu. Retrieved 18 August 2018.
  12. "PEP 397 — Python launcher for Windows — Python.org" . Retrieved 2017-06-07.