Tkinter is a
Python
Python may refer to:
Snakes
* Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia
** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia
* Python (mythology), a mythical serpent
Computing
* Python (pro ...
binding to the
Tk GUI
The GUI ( "UI" by itself is still usually pronounced . or ), graphical user interface, is a form of user interface that allows users to interact with electronic devices through graphical icons and audio indicator such as primary notation, inste ...
toolkit. It is the standard Python interface to the Tk GUI toolkit, and is Python's
''de facto'' standard GUI. Tkinter is included with standard
Linux
Linux ( or ) is a family of open-source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically packaged as a Linux distribution, which ...
,
Microsoft Windows
Windows is a group of several proprietary graphical operating system families developed and marketed by Microsoft. Each family caters to a certain sector of the computing industry. For example, Windows NT for consumers, Windows Server for serv ...
and
macOS
macOS (; previously OS X and originally Mac OS X) is a Unix operating system developed and marketed by Apple Inc. since 2001. It is the primary operating system for Apple's Mac computers. Within the market of desktop and lapt ...
installs of Python.
The name ''Tkinter'' comes from ''Tk interface''. Tkinter was written by
Steen Lumholt and
Guido van Rossum
Guido van Rossum (; born 31 January 1956) is a Dutch programmer best known as the creator of the Python programming language, for which he was the " benevolent dictator for life" (BDFL) until he stepped down from the position on 12 July 20 ...
, then later revised by Fredrik Lundh.
Tkinter is
free software
Free software or libre software is computer software distributed under terms that allow users to run the software for any purpose as well as to study, change, and distribute it and any adapted versions. Free software is a matter of liberty, no ...
released under a
Python license
The Python License is a deprecated permissive computer software license created by the Corporation for National Research Initiatives (CNRI). It was used for versions 1.6 and 2.0 of the Python programming language, both released in the year 2000 ...
.
Description
As with most other modern Tk bindings, Tkinter is implemented as a Python wrapper around a complete
Tcl
TCL or Tcl or TCLs may refer to:
Business
* TCL Technology, a Chinese consumer electronics and appliance company
**TCL Electronics, a subsidiary of TCL Technology
* Texas Collegiate League, a collegiate baseball league
* Trade Centre Limited ...
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
wxPython
wxPython is a wrapper for the cross-platform GUI API (often referred to as a "toolkit") wxWidgets (which is written in C++) for the Python programming language. It is one of the alternatives to Tkinter. It is implemented as a Python extensio ...
,
PyQt
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 mea ...
,
PySide
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 sup ...
,
Pygame
Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language.
History
Pygame was originally written by Pete Shinners ...
,
Pyglet
pyglet is a library for the Python programming language that provides an object-oriented application programming interface for the creation of games and other multimedia applications. pyglet runs on Microsoft Windows, macOS, and Linux; it is relea ...
, and
PyGTK
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 origi ...
.
Some definitions
Window
A window is an opening in a wall, door, roof, or vehicle that allows the exchange of light and may also allow the passage of sound and sometimes air. Modern windows are usually glazed or covered in some other transparent or translucent materia ...
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
In computing, a desktop environment (DE) is an implementation of the desktop metaphor made of a bundle of programs running on top of a computer operating system that share a common graphical user interface (GUI), sometimes described as a graphica ...
. 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. 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.
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:
#!/usr/bin/env python3
from tkinter import *
root = Tk() # Create the root (base) window
w = Label(root, text="Hello, world!") # Create a label with words
w.pack() # Put the label into the window
root.mainloop() # Start the event loop
For Python 2, the only difference is the word "tkinter" in the import command will be capitalized to "Tkinter".
Process
There are four stages to creating a widget
;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
Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of pro ...
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 python3
import tkinter as tk
class Application(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.grid()
self.createWidgets()
def createWidgets(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.master.title('Sample application')
app.mainloop()
* line 1:
Hashbang
In computing, a shebang is the character sequence consisting of the characters number sign and exclamation mark () at the beginning of a script. It is also called sharp-exclamation, sha-bang, hashbang, pound-bang, or hash-pling.
When a text fil ...
directive to the program launcher, allowing the selection of an appropriate interpreter executable, when self-executing.
* line 2: Imports the tkinter module into your program's namespace, but renames it as tk.
* line 5: The application class inherits from Tkinter's Frame class.
* line 7: Defines the function that sets up the Frame.
* line 8: Calls the constructor for the parent class, Frame.
* line 12: Defining the widgets.
* line 13: Creates a label, named MedialLabel with the text "Hello World".
* line 14: Sets the MedialLabel background colour to cyan.
* line 15: Places the label on the application so it is visible using the grid geometry manager method.
* line 16: Creates a button labeled “Quit”.
* line 17: Places the button on the application. Grid, place and pack are all methods of making the widget visible.
* line 20: The main program starts here by instantiating the Application class.
* line 21: This method call sets the title of the window to “Sample application”.
* line 22: Starts the application's main loop, waiting for mouse and keyboard events.
References
External links
*
Tkinter GUI Tutorial covers each widget individually.
TkDocs includes language-neutral and Python-specific information and a tutorial
*
*
{{Widget toolkits
Python (programming language) libraries
Tk (software)