On this page
Desktop GUI Development
Prev: Data Science & Machine Learning | Up: Popular Application Domains | Next: Automation, DevOps & Web Scraping
Rel: tkinter, turtle, queue, threading
Ref: docs.python.org — tkinter | docs.python.org — tkinter.ttk
Tags: #public #python3 #tutorials #domains #gui #tkinter #stdlib
Contents
- > Application Window & `tkinter.ttk` Widgets
- > Reactive State (`StringVar`) & Event Binding
- > Dialogs (`messagebox` & `filedialog`)
- > Non-Blocking Updates (`.after()` & Worker Threads)
Application Window & tkinter.ttk Widgets
[[stdlib_module/tkinter|tkinter]] is Python's built-in cross-platform GUI toolkit backed by Tcl/Tk. Prefer tkinter.ttk themed widgets and the .grid() geometry manager for clean, native-looking layouts.
import tkinter as tk
from tkinter import ttk
def build_counter_app() -> tk.Tk:
root = tk.Tk()
root.title("Stdlib Counter")
root.geometry("280x140")
frame = ttk.Frame(root, padding=16)
frame.grid(sticky="nsew")
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
count = tk.IntVar(value=0)
ttk.Label(frame, textvariable=count, font=("Helvetica", 24)).grid(row=0, column=0, columnspan=2, pady=8)
ttk.Button(frame, text="Increment", command=lambda: count.set(count.get() + 1)).grid(row=1, column=0, padx=4)
ttk.Button(frame, text="Reset", command=lambda: count.set(0)).grid(row=1, column=1, padx=4)
return root
# Run with:
# if __name__ == "__main__":
# build_counter_app().mainloop()
Reactive State (StringVar) & Event Binding
Tkinter variable wrappers (StringVar, IntVar, BooleanVar) synchronize UI controls automatically, and .bind() attaches keyboard or mouse handlers.
import tkinter as tk
from tkinter import ttk
def build_echo_app() -> tk.Tk:
root = tk.Tk()
root.title("Event Binding")
text_var = tk.StringVar()
entry = ttk.Entry(root, textvariable=text_var, width=30)
entry.pack(padx=12, pady=8)
output = ttk.Label(root, text="Press Enter to submit")
output.pack(padx=12, pady=8)
def on_submit(_event) -> None:
output.config(text=f"Submitted: {text_var.get()}")
text_var.set("")
entry.bind("<Return>", on_submit)
return root
Dialogs (messagebox & filedialog)
Built-in native OS dialogs live in tkinter.messagebox and tkinter.filedialog:
from tkinter import filedialog, messagebox
def open_and_confirm() -> str | None:
path = filedialog.askopenfilename(
title="Select a text file",
filetypes=[("Text files", "*.txt"), ("All files", "*.*")],
)
if path and messagebox.askyesno("Confirm", f"Load {path}?"):
return path
return None
Non-Blocking Updates (.after() & Worker Threads)
Tkinter is single-threaded: never call widget methods directly from background threads and never call time.sleep() on the main thread. Instead, push results from a worker thread into a [[stdlib_module/queue|queue.Queue]] and poll it safely from the UI thread using root.after(ms, callback).
import queue
import threading
import tkinter as tk
from tkinter import ttk
def start_background_job(root: tk.Tk, status_label: ttk.Label) -> None:
result_q: queue.Queue[str] = queue.Queue()
def worker() -> None:
total = sum(range(500_000))
result_q.put(f"Done: {total}")
def poll() -> None:
try:
status_label.config(text=result_q.get_nowait())
except queue.Empty:
root.after(50, poll)
threading.Thread(target=worker, daemon=True).start()
root.after(50, poll)
Linked from
- tuts/python3/tutorials
- tuts/python3/tutorials/popular_application_domains
- tuts/python3/tutorials/popular_application_domains/automation_devops_and_web_scraping
- tuts/python3/tutorials/popular_application_domains/data_science_and_machine_learning
- tuts/python3/tutorials/popular_application_domains/desktop_gui_development
Related notes
- tuts/python3/tutorials/popular_application_domains/automation_devops_and_web_scraping Linked here; links to this page; shared tags: #domains, #stdlib, #tutorials
- tuts/python3/tutorials/popular_application_domains/data_science_and_machine_learning Linked here; links to this page; shared tags: #domains, #stdlib, #tutorials
- tuts/python3/tutorials/popular_application_domains Linked here; links to this page; shared tags: #domains, #tutorials
- tuts/python3/stdlib_module/queue Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/threading Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/tkinter Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/turtle Linked here; shared tags: #stdlib
- tuts/python3/tutorials links to this page; shared tags: #tutorials
- tuts/python3/tutorials/popular_application_domains/web_development_and_apis shared tags: #domains, #stdlib, #tutorials
- tuts/python3/stdlib_module/tkinter/Button shared tags: #stdlib, #tkinter