On this page
tkinter
Prev: timeit | Up: stdlib_module | Next: token
Rel: wave, colorsys, gettext, locale, turtle
Ref: docs.python.org — tkinter
Tags: #public #python3 #stdlib
Import
import tkinter
Overview
Python's built-in cross-platform desktop GUI toolkit wrapping Tcl/Tk (Tk, ttk, Canvas, Text, StringVar, messagebox, filedialog). Reach for tkinter + tkinter.ttk for lightweight desktop utilities, prototypes, and zero-dependency desktop apps (such as custom terminals or local tools) without shipping Electron or Qt.
Quick Start
import tkinter as tk
from tkinter import ttk
def build_app() -> tk.Tk:
root = tk.Tk()
root.title("Counter")
count = tk.IntVar(value=0)
ttk.Label(root, textvariable=count, padding=12).pack()
ttk.Button(root, text="Increment", command=lambda: count.set(count.get() + 1)).pack()
return root
# Run on a desktop display:
# build_app().mainloop()
assert hasattr(tk, "Tk") and hasattr(ttk, "Button")
Implementation Walkthrough
1. Thread-Safe Background Updates with root.after() & queue.Queue
Never mutate Tk widgets from a worker thread—push updates into a queue.Queue and poll it on the main UI thread with root.after():
import queue
import tkinter as tk
def poll_queue(root: tk.Tk, q: queue.Queue[str], var: tk.StringVar) -> None:
try:
while True:
var.set(q.get_nowait())
except queue.Empty:
pass
root.after(50, poll_queue, root, q, var)
Gotchas & Boundaries
- Tkinter is strictly single-threaded: All widget creation and updates must run on the thread that created
tk.Tk()and runsmainloop(). - Prefer
tkinter.ttkwidgets over classictkwidgets for native macOS/Windows look-and-feel (use classictk.Canvasandtk.Textfor custom drawing/text grids). - Never mix
.pack()and.grid()inside the same parent container: Mixing them hangs or raisesTclError. - Headless CI/SSH environments lack a display: Guard GUI tests or skip when
DISPLAYis absent on Linux.
Common Functions
- tkinter.Button() —
class Button(master=None, cnf={}, **kw)— Button widget to trigger a callback command. - tkinter.Canvas() —
class Canvas(master=None, cnf={}, **kw)— Canvas widget to display graphical elements like lines or text. - tkinter.Entry() —
class Entry(master=None, cnf={}, **kw)— Single-line text entry widget. - tkinter.Frame() —
class Frame(master=None, cnf={}, **kw)— Frame widget which may contain other widgets and can have a 3D border. - tkinter.Label() —
class Label(master=None, cnf={}, **kw)— Widget which can display text and bitmaps. - tkinter.Tk() —
class Tk(screenName=None, baseName=None, className='Tk', useTk=True)— Toplevel widget of Tk which represents mostly the main window of an application.
Linked from
- tuts/python3
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/idlelib
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/timeit
- tuts/python3/stdlib_module/tkinter/Button
- tuts/python3/stdlib_module/tkinter/Canvas
- tuts/python3/stdlib_module/tkinter/Entry
- tuts/python3/stdlib_module/tkinter/Frame
- tuts/python3/stdlib_module/tkinter/Label
- tuts/python3/stdlib_module/tkinter/Tk
- tuts/python3/stdlib_module/token
- tuts/python3/stdlib_module/turtle
- tuts/python3/stdlib_module/turtledemo
- tuts/python3/tutorials/popular_application_domains
- tuts/python3/tutorials/popular_application_domains/desktop_gui_development
Related notes
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/timeit Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/tkinter/Button Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/tkinter/Canvas Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/tkinter/Entry Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/tkinter/Frame Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/tkinter/Label Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/tkinter/Tk Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/token Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/turtle Linked here; links to this page; shared tags: #stdlib