Skip to content

Commit 6597551

Browse files
committed
Sample code for the article on Tkinter
1 parent 755e016 commit 6597551

30 files changed

+516
-0
lines changed

python-gui-tkinter/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python GUI Programming: Your Tkinter Tutorial
2+
3+
This folder provides the code examples for the Real Python tutorial [Python GUI Programming: Your Tkinter Tutorial](https://realpython.com/python-gui-tkinter/).

python-gui-tkinter/address_form.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import tkinter as tk
2+
3+
root = tk.Tk()
4+
root.title("Address Entry Form")
5+
6+
frm_form = tk.Frame(relief=tk.SUNKEN, borderwidth=3)
7+
frm_form.pack()
8+
9+
field_labels = [
10+
"First Name:",
11+
"Last Name:",
12+
"Address Line 1:",
13+
"Address Line 2:",
14+
"City:",
15+
"State/Province:",
16+
"Postal Code:",
17+
"Country:",
18+
]
19+
20+
for index, text in enumerate(field_labels):
21+
tk.Label(
22+
master=frm_form,
23+
text=text,
24+
).grid(row=index, column=0, sticky="e")
25+
tk.Entry(
26+
master=frm_form,
27+
width=50,
28+
).grid(row=index, column=1)
29+
30+
frm_buttons = tk.Frame()
31+
frm_buttons.pack(fill=tk.X, ipadx=5, ipady=5)
32+
33+
btn_submit = tk.Button(master=frm_buttons, text="Submit")
34+
btn_submit.pack(side=tk.RIGHT, padx=10, ipadx=10)
35+
36+
btn_clear = tk.Button(master=frm_buttons, text="Clear")
37+
btn_clear.pack(side=tk.RIGHT, ipadx=10)
38+
39+
root.mainloop()

python-gui-tkinter/app.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import tkinter as tk
2+
3+
root = tk.Tk()
4+
5+
greeting = tk.Label(
6+
text="Hello, Tkinter",
7+
fg="white",
8+
bg="black",
9+
width=10,
10+
height=10,
11+
)
12+
greeting.pack()
13+
14+
root.mainloop()

python-gui-tkinter/button.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import tkinter as tk
2+
3+
root = tk.Tk()
4+
5+
button = tk.Button(
6+
text="Click me!",
7+
width=25,
8+
height=5,
9+
fg="red",
10+
)
11+
button.pack()
12+
13+
root.mainloop()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import tkinter as tk
2+
3+
root = tk.Tk()
4+
root.geometry("200x80")
5+
6+
counter = 0
7+
8+
9+
def handle_click(event):
10+
global counter
11+
counter += 1
12+
label.config(text="Click Count: " + str(counter))
13+
14+
15+
label = tk.Label(root, text="Click Count: 0")
16+
label.pack()
17+
18+
button = tk.Button(text="Click me!")
19+
button.pack()
20+
21+
22+
button.bind("<Button-1>", handle_click)
23+
24+
root.mainloop()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import tkinter as tk
2+
3+
root = tk.Tk()
4+
root.geometry("200x80")
5+
6+
counter = 0
7+
8+
9+
def handle_click():
10+
global counter
11+
counter += 1
12+
label.config(text="Click Count: " + str(counter))
13+
14+
15+
label = tk.Label(root, text="Click Count: 0")
16+
label.pack()
17+
18+
button = tk.Button(text="Click me!", command=handle_click)
19+
button.pack()
20+
21+
22+
root.mainloop()

python-gui-tkinter/editor.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import tkinter as tk
2+
3+
root = tk.Tk()
4+
root.title("Simple Text Editor")
5+
6+
frame = tk.Frame(root)
7+
frame.pack(side=tk.LEFT, fill=tk.Y)
8+
9+
tk.Button(
10+
frame,
11+
text="Open",
12+
).pack(fill=tk.X, padx=5, pady=5)
13+
tk.Button(
14+
frame,
15+
text="Save As...",
16+
).pack(fill=tk.X, padx=5, pady=5)
17+
18+
txt = tk.Text(root)
19+
txt.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)
20+
21+
root.mainloop()

python-gui-tkinter/editor_final.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import tkinter as tk
2+
from tkinter.filedialog import askopenfilename, asksaveasfilename
3+
4+
FILE_TYPES = [("Text Files", "*.txt"), ("All Files", "*.*")]
5+
6+
7+
root = tk.Tk()
8+
root.title("Simple Text Editor")
9+
10+
11+
def open_file():
12+
path = askopenfilename(filetypes=FILE_TYPES)
13+
if path:
14+
with open(path, mode="r", encoding="utf-8") as text_file:
15+
txt.delete("1.0", tk.END)
16+
txt.insert(tk.END, text_file.read())
17+
18+
19+
def save_file():
20+
path = asksaveasfilename(defaultextension=".txt", filetypes=FILE_TYPES)
21+
if path:
22+
with open(path, mode="w", encoding="utf-8") as text_file:
23+
text_file.write(txt.get("1.0", tk.END))
24+
25+
26+
frame = tk.Frame(root)
27+
frame.pack(side=tk.LEFT, fill=tk.Y)
28+
29+
tk.Button(
30+
frame,
31+
text="Open",
32+
command=open_file,
33+
).pack(fill=tk.X, padx=5, pady=5)
34+
tk.Button(
35+
frame,
36+
text="Save As...",
37+
command=save_file,
38+
).pack(fill=tk.X, padx=5, pady=5)
39+
40+
txt = tk.Text(root)
41+
txt.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)
42+
43+
44+
root.mainloop()

python-gui-tkinter/entry.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import tkinter as tk
2+
3+
root = tk.Tk()
4+
5+
label = tk.Label(text="Name")
6+
entry = tk.Entry()
7+
label.pack()
8+
entry.pack()
9+
10+
root.mainloop()

python-gui-tkinter/frame.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import tkinter as tk
2+
3+
root = tk.Tk()
4+
5+
frame = tk.Frame(master=root)
6+
frame.pack()
7+
8+
root.mainloop()

0 commit comments

Comments
 (0)