Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions python-gui-tkinter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Python GUI Programming: Your Tkinter Tutorial

This folder provides the code examples for the Real Python tutorial [Python GUI Programming: Your Tkinter Tutorial](https://realpython.com/python-gui-tkinter/).
39 changes: 39 additions & 0 deletions python-gui-tkinter/address_form.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import tkinter as tk

root = tk.Tk()
root.title("Address Entry Form")

frm_form = tk.Frame(relief=tk.SUNKEN, borderwidth=3)
frm_form.pack()

field_labels = [
"First Name:",
"Last Name:",
"Address Line 1:",
"Address Line 2:",
"City:",
"State/Province:",
"Postal Code:",
"Country:",
]

for index, text in enumerate(field_labels):
tk.Label(
master=frm_form,
text=text,
).grid(row=index, column=0, sticky="e")
tk.Entry(
master=frm_form,
width=50,
).grid(row=index, column=1)

frm_buttons = tk.Frame()
frm_buttons.pack(fill=tk.X, ipadx=5, ipady=5)

btn_submit = tk.Button(master=frm_buttons, text="Submit")
btn_submit.pack(side=tk.RIGHT, padx=10, ipadx=10)

btn_clear = tk.Button(master=frm_buttons, text="Clear")
btn_clear.pack(side=tk.RIGHT, ipadx=10)

root.mainloop()
14 changes: 14 additions & 0 deletions python-gui-tkinter/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import tkinter as tk

root = tk.Tk()

greeting = tk.Label(
text="Hello, Tkinter",
fg="white",
bg="black",
width=10,
height=10,
)
greeting.pack()

root.mainloop()
13 changes: 13 additions & 0 deletions python-gui-tkinter/button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import tkinter as tk

root = tk.Tk()

button = tk.Button(
text="Click me!",
width=25,
height=5,
fg="red",
)
button.pack()

root.mainloop()
24 changes: 24 additions & 0 deletions python-gui-tkinter/click_counter_v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import tkinter as tk

root = tk.Tk()
root.geometry("200x80")

counter = 0


def handle_click(event):
global counter
counter += 1
label.config(text="Click Count: " + str(counter))


label = tk.Label(root, text="Click Count: 0")
label.pack()

button = tk.Button(text="Click me!")
button.pack()


button.bind("<Button-1>", handle_click)

root.mainloop()
22 changes: 22 additions & 0 deletions python-gui-tkinter/click_counter_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import tkinter as tk

root = tk.Tk()
root.geometry("200x80")

counter = 0


def handle_click():
global counter
counter += 1
label.config(text="Click Count: " + str(counter))


label = tk.Label(root, text="Click Count: 0")
label.pack()

button = tk.Button(text="Click me!", command=handle_click)
button.pack()


root.mainloop()
21 changes: 21 additions & 0 deletions python-gui-tkinter/editor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import tkinter as tk

root = tk.Tk()
root.title("Simple Text Editor")

frame = tk.Frame(root)
frame.pack(side=tk.LEFT, fill=tk.Y)

tk.Button(
frame,
text="Open",
).pack(fill=tk.X, padx=5, pady=5)
tk.Button(
frame,
text="Save As...",
).pack(fill=tk.X, padx=5, pady=5)

txt = tk.Text(root)
txt.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)

root.mainloop()
44 changes: 44 additions & 0 deletions python-gui-tkinter/editor_final.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import tkinter as tk
from tkinter.filedialog import askopenfilename, asksaveasfilename

FILE_TYPES = [("Text Files", "*.txt"), ("All Files", "*.*")]


root = tk.Tk()
root.title("Simple Text Editor")


def open_file():
path = askopenfilename(filetypes=FILE_TYPES)
if path:
with open(path, mode="r", encoding="utf-8") as text_file:
txt.delete("1.0", tk.END)
txt.insert(tk.END, text_file.read())


def save_file():
path = asksaveasfilename(defaultextension=".txt", filetypes=FILE_TYPES)
if path:
with open(path, mode="w", encoding="utf-8") as text_file:
text_file.write(txt.get("1.0", tk.END))


frame = tk.Frame(root)
frame.pack(side=tk.LEFT, fill=tk.Y)

tk.Button(
frame,
text="Open",
command=open_file,
).pack(fill=tk.X, padx=5, pady=5)
tk.Button(
frame,
text="Save As...",
command=save_file,
).pack(fill=tk.X, padx=5, pady=5)

txt = tk.Text(root)
txt.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)


root.mainloop()
10 changes: 10 additions & 0 deletions python-gui-tkinter/entry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import tkinter as tk

root = tk.Tk()

label = tk.Label(text="Name")
entry = tk.Entry()
label.pack()
entry.pack()

root.mainloop()
8 changes: 8 additions & 0 deletions python-gui-tkinter/frame.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import tkinter as tk

root = tk.Tk()

frame = tk.Frame(master=root)
frame.pack()

root.mainloop()
19 changes: 19 additions & 0 deletions python-gui-tkinter/frame_borders.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import tkinter as tk

EFFECTS = (
tk.FLAT,
tk.SUNKEN,
tk.RAISED,
tk.GROOVE,
tk.RIDGE,
)

root = tk.Tk()

for effect in EFFECTS:
frame = tk.Frame(master=root, relief=effect, borderwidth=5)
frame.pack(side=tk.LEFT)
label = tk.Label(master=frame, text=effect)
label.pack()

root.mainloop()
17 changes: 17 additions & 0 deletions python-gui-tkinter/frames_v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import tkinter as tk

root = tk.Tk()

frame_a = tk.Frame(master=root)
frame_b = tk.Frame(master=root)

label_a = tk.Label(master=frame_a, text="I'm in Frame A")
label_a.pack()

label_b = tk.Label(master=frame_b, text="I'm in Frame B")
label_b.pack()

frame_a.pack()
frame_b.pack()

root.mainloop()
18 changes: 18 additions & 0 deletions python-gui-tkinter/frames_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import tkinter as tk

root = tk.Tk()

frame_a = tk.Frame(master=root)
frame_b = tk.Frame(master=root)

label_a = tk.Label(master=frame_a, text="I'm in Frame A")
label_a.pack()

label_b = tk.Label(master=frame_b, text="I'm in Frame B")
label_b.pack()

# Swap the order of `frame_a` and `frame_b`
frame_b.pack()
frame_a.pack()

root.mainloop()
12 changes: 12 additions & 0 deletions python-gui-tkinter/grid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import tkinter as tk

root = tk.Tk()

for i in range(3):
for j in range(3):
frame = tk.Frame(master=root, relief=tk.RAISED, borderwidth=1)
frame.grid(row=i, column=j)
label = tk.Label(master=frame, text=f"Row {i}\nColumn {j}")
label.pack()

root.mainloop()
13 changes: 13 additions & 0 deletions python-gui-tkinter/grid_centered_labels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import tkinter as tk

root = tk.Tk()
root.columnconfigure(0, minsize=250)
root.rowconfigure([0, 1], minsize=100)

label1 = tk.Label(text="A")
label1.grid(row=0, column=0)

label2 = tk.Label(text="B")
label2.grid(row=1, column=0)

root.mainloop()
15 changes: 15 additions & 0 deletions python-gui-tkinter/grid_expand.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import tkinter as tk

root = tk.Tk()

for i in range(3):
root.columnconfigure(i, weight=1, minsize=75)
root.rowconfigure(i, weight=1, minsize=50)

for j in range(0, 3):
frame = tk.Frame(master=root, relief=tk.RAISED, borderwidth=1)
frame.grid(row=i, column=j, padx=5, pady=5)
label = tk.Label(master=frame, text=f"Row {i}\nColumn {j}")
label.pack(padx=5, pady=5)

root.mainloop()
12 changes: 12 additions & 0 deletions python-gui-tkinter/grid_padding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import tkinter as tk

root = tk.Tk()

for i in range(3):
for j in range(3):
frame = tk.Frame(master=root, relief=tk.RAISED, borderwidth=1)
frame.grid(row=i, column=j, padx=5, pady=5)
label = tk.Label(master=frame, text=f"Row {i}\nColumn {j}")
label.pack(padx=5, pady=5)

root.mainloop()
18 changes: 18 additions & 0 deletions python-gui-tkinter/grid_sticky_combined.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import tkinter as tk

root = tk.Tk()

root.rowconfigure(0, minsize=50)
root.columnconfigure([0, 1, 2, 3], minsize=50)

label1 = tk.Label(text="1", bg="black", fg="white")
label2 = tk.Label(text="2", bg="black", fg="white")
label3 = tk.Label(text="3", bg="black", fg="white")
label4 = tk.Label(text="4", bg="black", fg="white")

label1.grid(row=0, column=0)
label2.grid(row=0, column=1, sticky="ew")
label3.grid(row=0, column=2, sticky="ns")
label4.grid(row=0, column=3, sticky="nsew")

root.mainloop()
13 changes: 13 additions & 0 deletions python-gui-tkinter/grid_sticky_corners.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import tkinter as tk

root = tk.Tk()
root.columnconfigure(0, minsize=250)
root.rowconfigure([0, 1], minsize=100)

label1 = tk.Label(text="A")
label1.grid(row=0, column=0, sticky="ne")

label2 = tk.Label(text="B")
label2.grid(row=1, column=0, sticky="sw")

root.mainloop()
13 changes: 13 additions & 0 deletions python-gui-tkinter/grid_sticky_north.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import tkinter as tk

root = tk.Tk()
root.columnconfigure(0, minsize=250)
root.rowconfigure([0, 1], minsize=100)

label1 = tk.Label(text="A")
label1.grid(row=0, column=0, sticky="n")

label2 = tk.Label(text="B")
label2.grid(row=1, column=0, sticky="n")

root.mainloop()
21 changes: 21 additions & 0 deletions python-gui-tkinter/keypress.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import tkinter as tk

root = tk.Tk()
root.geometry("200x80")


label = tk.Label(root, text="", font=("Arial", 16))
label.pack(pady=20)

current_text = []


def handle_keypress(event):
if event.char.isprintable():
current_text.append(event.char)
label.config(text="".join(current_text))


root.bind("<KeyPress>", handle_keypress)

root.mainloop()
Loading