Tkinter - Wikipedia Jump to content Main menu Main menu move to sidebar hide Navigation Main page Contents Current events Random article About Wikipedia Contact us Contribute Help Learn to edit Community portal Recent changes Upload file Special pages Search Search Appearance Don…
Tkinter - Wikipedia Jump to content Main menu Main menu move to sidebar hide Navigation Main page Contents Current events Random article About Wikipedia Contact us Contribute Help Learn to edit Community portal Recent changes Upload file Special pages Search Search Appearance Donate Create account Log in Personal tools Donate Create account Log in Contents move to sidebar hide (Top) 1 Description Toggle Description subsection 1.1 Definitions 1.1.1 Widget 1.1.2 Frame 1.1.3 Child and parent 2 Minimal application Toggle Minimal application subsection 2.1 Process 2.2 Simple application 3 See also 4 References 5 External links Toggle the table of contents Tkinter 22 languages العربية বাংলা Català Čeština Deutsch Esperanto Español Eesti فارسی Français עברית Bahasa Indonesia Italiano 日本語 한국어 Polski Português Русский Српски / srpski Türkçe Українська 中文 Edit links Article Talk English Read Edit View history Tools Tools move to sidebar hide Actions Read Edit View history General What links here Related changes Upload file Permanent link Page information Cite this page Get shortened URL Switch to legacy parser Print/export Download as PDF Printable version In other projects Abstract Wikipedia Wikimedia Commons Wikibooks Wikidata item Appearance move to sidebar hide From Wikipedia, the free encyclopedia Python binding to the Tk GUI toolkit "},"logo caption":{"wt":""},"logo alt":{"wt":""},"logo size":{"wt":""},"collapsible":{"wt":"<!-- Any text here will collapse the screenshot. -->"},"screenshot":{"wt":"Python's_IDLE.png"},"screenshot size":{"wt":""},"screenshot alt":{"wt":"The [[IDLE]] Python editor"},"caption":{"wt":"The [[IDLE]] Python editor"},"other_names":{"wt":""},"author":{"wt":""},"developer":{"wt":""},"released":{"wt":"<!-- {{Start date and age|YYYY|MM|DD|df=yes/no}} -->"},"ver layout":{"wt":"<!-- simple (default) or stacked -->"},"discontinued":{"wt":"<!-- Set to yes, if software is discontinued, otherwise omit. -->"},"latest release version":{"wt":""},"latest release date":{"wt":"<!-- {{Start date and age|YYYY|MM|DD|df=yes/no}} -->"},"latest preview version":{"wt":""},"latest preview date":{"wt":"<!-- {{Start date and age|YYYY|MM|DD|df=yes/no}} -->"},"repo":{"wt":"<!-- {{URL|example.org}} -->"},"qid":{"wt":""},"programming language":{"wt":""},"middleware":{"wt":""},"engine":{"wt":"<!-- or |engines= -->"},"operating system":{"wt":""},"platform":{"wt":""},"included with":{"wt":""},"replaces":{"wt":""},"replaced_by":{"wt":""},"service_name":{"wt":""},"size":{"wt":""},"standard":{"wt":""},"language":{"wt":""},"language count":{"wt":"<!-- Number only -->"},"language footnote":{"wt":""},"genre":{"wt":""},"license":{"wt":"[[Python license]]"},"website":{"wt":"https://docs.python.org/3/library/tkinter.html"},"AsOf":{"wt":""}},"i":0}}]}'>Tkinter IDLE</a> Python editor","txt":"The IDLE Python editor"}]]}'>The IDLE Python editor License Python license Website https://docs.python.org/3/library/tkinter.html Tkinter is a binding to the Tk GUI toolkit for Python. 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,[3] then later revised by Fredrik Lundh.[4] Tkinter is free software released under a Python license.[5] Description [edit] 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. The official Python release uses Tcl/Tk 8.6 (rather than the more up-to-date 9.0).[6] There are several popular GUI library alternatives available, such as Kivy, Pygame, Pyglet, PyGObject, PyQt, PySide, and wxPython. Definitions [edit] from tkinter import * def calculate(): price = float(entry_price.get()) qty = float(entry_qty.get()) total = price * qty label_result.config(text="Total: " + str(total)) app = Tk() app.title("Facturation App") Label(app, text="Prix").pack() entry_price = Entry(app) entry_price.pack() Label(app, text="Quantité").pack() entry_qty = Entry(app) entry_qty.pack() Button(app, text="Calculer", command=calculate).pack() label_result = Label(app, text="") label_result.pack() app.mainloop() Widget [edit] The generic term for any of the building blocks that make up an application in a graphical user interface. Core widgets: Containers: frame labelframe toplevel paned window. Buttons: button radiobutton checkbutton (checkbox) menubutton. Text widgets: label, message text Entry widgets: scale scrollbar listbox slider spinbox entry (singleline) optionmenu text (multiline) 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.[7][8] 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.[9] Frame [edit] 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 [edit] 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. Minimal application [edit] Below is a minimal Python 3 Tkinter application with one widget:[10] #!/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".[11] Process [edit] There are four stages to creating a widget[12] Create Create it within a frame Configure Change the widget's 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 [edit] 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 python3 import tkinter as tk class Application(tk.Frame): """Application holds state for the whole app.""" 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 = Application() app.root = tk.Tk() app.root.title("Sample application") app.mainloop() line 1: Hashbang directive to the program launcher, allowing the selection of an appropriate interpreter executable, when self-executing.[13] 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: Creates main window app.root as a Tk object. line 22: This method call sets the title of the window to “Sample application”. line 23: Starts the application's main loop, waiting for mouse and keyboard events. See also [edit] IDLE — integrated development environment in Python, written exclusively using Tkinter References [edit] ↑ "Tkinter — Python interface to Tcl/Tk — Python v2.6.1 documentation". Retrieved 2009-03-12. ↑ "Tkinter - Pythoninfo Wiki". ↑ tkinter—Python interface to Tcl/Tk—Python 3.9.10 Documentation ↑ Shipman, John W. (2010-12-12), Tkinter reference: a GUI for Python, New Mexico Tech Computer Center, retrieved 2012-01-11 ↑ "Tkinter - Tkinter Wiki". Archived from the original on 2013-11-13. Retrieved 2013-11-13. ↑ "tkinter — Python interface to Tcl/Tk". Python 3.14.5 documentation. Retrieved 2026-05-30. ↑ "Python issue #2983, "Ttk support for Tkinter"". ↑ "Python subversion revision 69051, which resolves issue #2983 by adding the ttk module". ↑ "Tkinter ttk widgets - Python Tutorial". CodersLegacy. Retrieved 2022-01-13. ↑ "Tkinter 8.5 reference: a GUI for Python". ↑ Fleck, Dan. "Tkinter – GUIs in Python" (PDF). CS112. George Mason University. Retrieved 18 August 2018. ↑ Klein, Bernd. "GUI Programming with Python: Events and Binds". www.python-course.eu. Retrieved 18 August 2018. ↑ "PEP 397 — Python launcher for Windows — Python.org". Retrieved 2017-06-07. External links [edit] TkInter, Python Wiki Tkinter GUI Tutorial, covers each widget individually. TkDocs: includes language-neutral and Python-specific information and a tutorial John Shipman, Tkinter 8.5 reference: a GUI for Python Ferg, Stephen, Thinking in Tkinter v t e List of widget toolkits Low-level platform-specific On AmigaOS Intuition On Classic Mac OS, macOS Macintosh Toolbox Carbon On Windows Windows API UWP WinRT On Unix Xlib XCB Wayland On BeOS, Haiku BeOS API On Android CLI Xamarin.Android Low Level Cross-platform C GDK Simple DirectMedia Layer Java JOGL LWJGL High-level, platform-specific On AmigaOS BOOPSI MUI ReAction GUI Zune On Classic Mac OS, macOS Object Pascal MacApp Objective-C, Swift Cocoa Cocoa Touch C++ MacApp PowerPlant THINK C CLI Xamarin.Mac Xamarin.iOS On Windows CLI Windows Forms XAML Windows Presentation Foundation Windows UI Library Silverlight Microsoft XNA C++ MFC Active Template Library Windows Template Library Object Windows Library Object Pascal Visual Component Library On Unix and X11 Athena (Xaw) LessTif Motif OLIT XForms XView High-level, cross-platform C Enlightenment Foundation Libraries GTK IUP XForms XVT C++ Bedrock CEGUI Component Library for Cross Platform FLTK FOX toolkit OpenGL User Interface Library gtkmm JUCE Qt Rogue Wave Views TnFOX U++ Wt wxWidgets Simple and Fast Multimedia Library Objective-C GNUstep CLI Gtk# UIML MonoGame Moonlight Xamarin.Forms .NET MAUI Adobe Flash Apache Flex Go Fyne Haskell wxHaskell Java Abstract Window Toolkit FXML JavaFX Qt Jambi Swing Standard Widget Toolkit Google Web Toolkit Lightweight User Interface Toolkit JavaScript Dojo Echo Ext JS Closure jQuery UI OpenUI5 Qooxdoo React Native YUI Common Lisp CAPI CLIM Common Graphics Lua IUP Pascal Lazarus Component Library Object Pascal Component Library for Cross Platform fpGUI IP Pascal FireMonkey Perl Perl/Tk wxPerl PHP PHP-GTK wxPHP Python Tkinter Kivy PySide PyQt PyGTK wxPython Ruby Shoes QtRuby Tcl Tcl/Tk XML GladeXML Lively Kernel Extensible A…