Tkinter Explained

Tkinter
Screenshot Alt:The IDLE Python editor
Engines:-->
License:Python license

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.

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

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

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.

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:[7]

  1. !/usr/bin/env python3

from tkinter import *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".[8]

Process

There are four stages to creating a widget[9]

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):

    1. !/usr/bin/env python3

    import tkinter as tk

    class Application(tk.Frame):

    def __init__(self, root=None): tk.Frame.__init__(self, root) 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 = Applicationapp.root.title('Sample application')app.mainloop

    See also

    External links

    Notes and References

    1. Web site: Tkinter — Python interface to Tcl/Tk — Python v2.6.1 documentation . 2009-03-12.
    2. Web site: Tkinter - Pythoninfo Wiki.
    3. Web site: Tkinter - Tkinter Wiki . 2013-11-13 . dead . https://web.archive.org/web/20131113222939/http://tkinter.unpythonic.net/wiki/Tkinter . 2013-11-13 .
    4. Web site: Python issue #2983, "Ttk support for Tkinter" .
    5. Web site: Python subversion revision 69051, which resolves issue #2983 by adding the ttk module.
    6. Web site: Tkinter ttk widgets - Python Tutorial. 2022-01-13. CodersLegacy. en-us.
    7. Web site: Tkinter 8.5 reference: a GUI for Python .
    8. Web site: Fleck . Dan . Tkinter – GUIs in Python . CS112 . George Mason University . 18 August 2018.
    9. Web site: Klein . Bernd . GUI Programming with Python: Events and Binds . www.python-course.eu . 18 August 2018.
    10. Web site: PEP 397 — Python launcher for Windows — Python.org . 2017-06-07.