100+ Tkinter Python Program Examples – Practical Code Snippets
Tkinter remains the standard GUI toolkit for Python, enabling developers to build functional desktop applications without external dependencies. This collection of tkinter python program examples serves as a practical guide for beginners and a reference for experienced coders. Each example demonstrates core concepts, from simple window creation to complex event handling, all within a few lines of readable code.
Setting Up Your First Tkinter Window
The foundation of any tkinter python program example is the main application window. You initialize Tk, set a title, define dimensions, and start the main event loop. This basic structure is the starting point for almost every graphical interface you will ever build in Python.
import tkinter as tk
root = tk.Tk()
root.title("First App")
root.geometry("300x200")
root.mainloop()
Managing Layout with Frames and Grid
As interfaces grow, organizing widgets becomes critical. The grid geometry manager allows you to position elements in rows and columns, creating structured layouts. The following tkinter python program example builds a simple login form with labels, entry fields, and a button aligned neatly on a grid.
import tkinter as tk root = tk.Tk() tk.Label(root, text="Username").grid(row=0, sticky="w") tk.Label(root, text="Password").grid(row=1, sticky="w") entry_user = tk.Entry(root) entry_pass = tk.Entry(root, show="*") entry_user.grid(row=0, column=1) entry_pass.grid(row=1, column=1) tk.Button(root, text="Login").grid(columnspan=2) root.mainloop()
Handling User Input and Events
Interactivity defines a GUI. You capture user input by binding functions to events, such as button clicks or key presses. This next tkinter python program example shows how to retrieve text from an entry widget and display it in a label when a button is pressed.
import tkinter as tk
def show_text():
output_label.config(text=f"Hello, {name_entry.get()}!")
root = tk.Tk()
name_entry = tk.Entry(root)
output_label = tk.Label(root, text="")
button = tk.Button(root, text="Greet", command=show_text)
name_entry.pack(pady=10)
button.pack()
output_label.pack()
root.mainloop()
Building Dynamic Forms with Variable Tracing
Using BooleanVars and IntVars
For forms that need to react to changes, tkinter variables offer a powerful solution. By tracing changes to a BooleanVar or IntVar, you can dynamically enable or disable widgets. This pattern is essential for creating conditional logic within your tkinter python program examples.
import tkinter as tk
root = tk.Tk()
var = tk.BooleanVar()
check = tk.Checkbutton(root, text="Enable", variable=var)
text_field = tk.Entry(root, state=tk.DISABLED)
def toggle(*args):
if var.get():
text_field.config(state=tk.NORMAL)
else:
text_field.config(state=tk.DISABLED)
var.trace("w", toggle)
check.pack()
text_field.pack()
root.mainloop()
Canvas and Drawing Applications
When you need to render graphics or allow drawing, the Canvas widget is your tool. It provides a blank area where you can create shapes, lines, and even embed other widgets. The following tkinter python program example lets users draw circles by clicking and dragging the mouse.
import tkinter as tk
root = tk.Tk()
canvas = tk.Canvas(root, bg="white", width=400, height=400)
canvas.pack()
def draw(event):
x, y = event.x, event.y
canvas.create_oval(x-10, y-10, x+10, y+10, fill="blue")
canvas.bind("", draw)
root.mainloop()
Integrating File Dialogs
Real-world applications often need to read or save files. Tkinter simplifies this with built-in dialogs that integrate seamlessly with the operating system. This final tkinter python program example opens a file dialog to select a text file and then displays its content in a text widget.
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
text_box = tk.Text(root)
def open_file():
path = filedialog.askopenfilename(filetypes=[("Text Files", "*.txt")])
if path:
with open(path) as f:
text_box.insert(tk.END, f.read())
tk.Button(root, text="Open", command=open_file).pack()
text_box.pack()
root.mainloop()