8000 Add lectures on GUI development. · DORELAS/complete-python-course@40d97af · GitHub
[go: up one dir, main page]

Skip to content

Commit 40d97af

Browse files
committed
Add lectures on GUI development.
1 parent 07b6fed commit 40d97af

File tree

15 files changed

+983
-0
lines changed

15 files changed

+983
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Getting Tkinter
2+
3+
Tkinter should be installed already. Try to run this in IDLE:
4+
5+
```
6+
import tkinter
7+
tkinter._test()
8+
```
9+
10+
If that fails, read on...
11+
12+
## Installing Tkinter on Windows
13+
14+
Tkinter comes with Python on Windows, if you selected the appropriate option in the official Windows installer.
15+
16+
If you have IDLE on Windows, you have Tkinter.
17+
18+
If not, run th 2364 e installer again making sure that Tcl/Tk is enabled in the installation.
19+
20+
Instructions: https://stackoverflow.com/a/50027385/1587271
21+
22+
## Installing Tkinter on Mac
23+
24+
I recommend installing Python via the official installer, which comes with Tkinter.
25+
26+
## Installing Tkinter on Ubuntu
27+
28+
Enable your virtual environment, and then run:
29+
30+
```
31+
sudo apt-get install python3-tk
32+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import tkinter as tk
2+
from tkinter import ttk
3+
4+
5+
def greet():
6+
print("Hello, World!")
7+
8+
9+
root = tk.Tk()
10+
root.title("Hello")
11+
12+
greet_button = ttk.Button(root, text="Greet", command=greet)
13+
greet_button.pack(side="left", fill="x", expand=True)
14+
15+
quit_button = ttk.Button(root, text="Quit", command=root.destroy)
16+
quit_button.pack(side="left", fill="x", expand=True) # could use side="right"
17+
18+
root.mainloop()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import tkinter as tk
2+
from tkinter import ttk
3+
4+
5+
def greet():
6+
# The get() method is used to fetch the value of a StringVar() instance.
7+
# If user_name is empty, print Hello, World!
8+
print(f"Hello, {user_name.get() or 'World'}!")
9+
10+
11+
root = tk.Tk()
12+
root.title("Greeter")
13+
14+
# Here we create an instances of the StringVar() class, which is to track the content of widgets
15+
user_name = tk.StringVar()
16+
17+
18+
name_label = ttk.Label(root, text="Name: ")
19+
name_label.pack(side="left", padx=(0, 10))
20+
name_entry = ttk.Entry(root, width=15, textvariable=user_name)
21+
name_entry.pack(side="left")
22+
name_entry.focus()
23+
24+
greet_button = ttk.Button(root, text="Greet", command=greet)
25+
greet_button.pack(side="left", fill="x", expand=True)
26+
27+
root.mainloop()
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import tkinter as tk
2+
3+
# -- Aligning with `side` ---
4+
5+
root = tk.Tk()
6+
7+
tk.Label(root, text="Label 1", bg="green").pack(side="left")
8+
tk.Label(root, text="Label 2", bg="red").pack(side="top")
9+
10+
root.mainloop()
11+
12+
# -- Filling in one direction --
13+
14+
root = tk.Tk()
15+
16+
tk.Label(root, text="Label 1", bg="green").pack(side="left", fill="y")
17+
tk.Label(root, text="Label 2", bg="red").pack(side="top", fill="x")
18+
19+
root.mainloop()
20+
21+
# -- Filling in both directions --
22+
23+
root = tk.Tk()
24+
25+
tk.Label(root, text="Label 1", bg="green").pack(side="left", fill="both")
26+
tk.Label(root, text="Label 2", bg="red").pack(side="top", fill="both")
27+
28+
root.mainloop()
29+
30+
31+
# -- Even if either label doesn't fill --
32+
33+
root = tk.Tk()
34+
35+
tk.Label(root, text="Label 1", bg="green").pack(side="left")
36+
tk.Label(root, text="Label 2", bg="red").pack(side="top", fill="both")
37+
38+
root.mainloop()
39+
40+
41+
root = tk.Tk()
42+
43+
tk.Label(root, text="Label 1", bg="green").pack(side="left", fill="both")
44+
tk.Label(root, text="Label 2", bg="red").pack(side="top")
45+
46+
root.mainloop()
47+
48+
# -- expand can make it grow as much as possible. It won't hide other widgets, but other widgets will be compressed --
49+
50+
root = tk.Tk()
51+
52+
tk.Label(root, text="Label 1", bg="green").pack(side="left", expand=True, fill="both")
53+
tk.Label(root, text="Label 2", bg="red").pack(side="top")
54+
55+
root.mainloop()
56+
57+
58+
# -- expanding two widgets means they share the available space evenly --
59+
60+
root = tk.Tk()
61+
62+
tk.Label(root, text="Label 2", bg="red").pack(side="top", expand=True, fill="both")
63+
tk.Label(root, text="Label 2", bg="red").pack(side="top", expand=True, fill="both")
64+
65+
root.mainloop()
66+
67+
68+
# -- whichever side comes first gets expansion priority --
69+
70+
root = tk.Tk()
71+
72+
tk.Label(root, text="Label left", bg="green").pack(
73+
side="left", expand=True, fill="both"
74+
)
75+
tk.Label(root, text="Label top", bg="red").pack(side="top", expand=True, fill="both")
76+
tk.Label(root, text="Label top", bg="red").pack(side="top", expand=True, fill="both")
77+
78+
root.mainloop()
79+
80+
root = tk.Tk()
81+
82+
tk.Label(root, text="Label top", bg="red").pack(side="top", expand=True, fill="both")
83+
tk.Label(root, text="Label top", bg="red").pack(side="top", expand=True, fill="both")
84+
tk.Label(root, text="Label left", bg="green").pack(
85+
side="left", expand=True, fill="both"
86+
)
87+
88+
root.mainloop()
89+
90+
# As you can see, even though we specificied `side="left"`, the last label was still underneath.
91+
# We can't just use packing on the root to take care of all our layout needs.
92+
# Hence, Tkinter has `Frame`, which is a container for other widgets.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import tkinter as tk
2+
from tkinter import ttk, filedialog
3+
4+
5+
def create_file():
6+
text_area = tk.Text(notebook)
7+
text_area.pack(fill="both", expand=True)
8+
9+
notebook.add(text_area, text="Untitled")
10+
notebook.pack(fill="both", expand=True)
11+
notebook.select(text_area)
12+
13+
14+
def save_file():
15+
file_path = filedialog.asksaveasfilename()
16+
17+
try:
18+
filename = file_path.split("/")[-1]
19+
current = root.nametowidget(notebook.select())
20+
content = current.get("1.0", "end-1c")
21+
22+
with open(file_path, "w") as file:
23+
file.write(content)
24+
25+
except (AttributeError, FileNotFoundError):
26+
print("Save operation cancelled")
27+
return
28+
29+
notebook.tab("current", text=filename)
30+
31+
32+
root = tk.Tk()
33+
root.title("Teclado Text Editor")
34+
root.option_add("*tearOff", False)
35+
36+
main = ttk.Frame(root)
37+
main.pack(fill="both", expand=True, padx=(1), pady=(4, 0))
38+
39+
menubar = tk.Menu(root)
40+
root.config(menu=menubar)
41+
42+
file_menu = tk.Menu(menubar)
43+
44+
menubar.add_cascade(menu=file_menu, label="File")
45+
46+
file_menu.add_command(label="New", command=create_file)
47+
file_menu.add_command(label="Save", command=save_file)
48+
49+
notebook = ttk.Notebook(main)
50+
51+
create_file()
52+
53+
root.mainloop()
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import tkinter as tk
2+
from tkinter import ttk, filedialog
3+
4+
5+
def create_file():
6+
text_area = tk.Text(notebook)
7+
text_area.pack(fill="both", expand=True)
8+
9+
notebook.add(text_area, text="Untitled")
10+
notebook.pack(fill="both", expand=True)
11+
notebook.select(text_area)
12+
13+
14+
def open_file():
15+
file_path = filedialog.askopenfilename()
16+
17+
try:
18+
filename = file_path.split("/")[-1]
19+
20+
with open(file_path, "r") as file:
21+
content = file.read()
22+
23+
except (AttributeError, FileNotFoundError):
24+
print("Open operation cancelled")
25+
return
26+
27+
text_area = tk.Text(notebook)
28+
text_area.insert("end", content)
29+
text_area.pack(fill="both", expand=True)
30+
31+
notebook.add(text_area, text=filename)
32+
notebook.pack(fill="both", expand=True)
33+
notebook.select(text_area)
34+
35+
36+
def save_file():
37+
file_path = filedialog.asksaveasfilename()
38+
39+
try:
40+
filename = file_path.split("/")[-1]
41+
current = root.nametowidget(notebook.select())
42+
57AE content = current.get("1.0", "end-1c")
43+
44+
with open(file_path, "w") as file:
45+
file.write(content)
46+
47+
except (AttributeError, FileNotFoundError):
48+
print("Save operation cancelled")
49+
return
50+
51+
notebook.tab("current", text=filename)
52+
53+
54+
root = tk.Tk()
55+
root.title("Teclado Text Editor")
56+
root.option_add("*tearOff", False)
57+
58+
main = ttk.Frame(root)
59+
main.pack(fill="both", expand=True, padx=(1), pady=(4, 0))
60+
61+
menubar = tk.Menu(root)
62+
root.config(menu=menubar)
63+
64+
file_menu = tk.Menu(menubar)
65+
66+
menubar.add_cascade(menu=file_menu, label="File")
67+
68+
file_menu.add_command(label="New", command=create_file)
69+
file_menu.add_command(label="Open...", command=open_file)
70+
file_menu.add_command(label="Save", command=save_file)
71+
72+
notebook = ttk.Notebook(main)
73+
74+
create_file()
75+
76+
root.mainloop()
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import tkinter as tk
2+
from tkinter import ttk, filedialog
3+
4+
5+
def create_file():
6+
text_area = tk.Text(notebook)
7+
text_area.pack(fill="both", expand=True)
8+
9+
notebook.add(text_area, text="Untitled")
10+
notebook.pack(fill="both", expand=True)
11+
notebook.select(text_area)
12+
13+
14+
def open_file():
15+
file_path = filedialog.askopenfilename()
16+
17+
try:
18+
filename = file_path.split("/")[-1]
19+
20+
with open(file_path, "r") as file:
21+
content = file.read()
22+
23+
except (AttributeError, FileNotFoundError):
24+
print("Open operation cancelled")
25+
return
26+
27+
text_area = tk.Text(notebook)
28+
text_area.insert("end", content)
29+
text_area.pack(fill="both", expand=True)
30+
31+
notebook.add(text_area, text=filename)
32+
notebook.pack(fill="both", expand=True)
33+
notebook.select(text_area)
34+
35+
36+
def save_file():
37+
file_path = filedialog.asksaveasfilename()
38+
39+
try:
40+
filename = file_path.split("/")[-1]
41+
current = root.nametowidget(notebook.select())
42+
content = current.get("1.0", "end-1c")
43+
44+
with open(file_path, "w") as file:
45+
file.write(content)
46+
47+
except (AttributeError, FileNotFoundError):
48+
print("Save operation cancelled")
49+
return
50+
51+
notebook.tab("current", text=filename)
52+
53+
54+
root = tk.Tk()
55+
root.title("Teclado Text Editor")
56+
root.option_add("*tearOff", False)
57+
58+
main = ttk.Frame(root)
59+
main.pack(fill="both", expand=True, padx=(1), pady=(4, 0))
60+
61+
menubar = tk.Menu(root)
62+
root.config(menu=menubar)
63+
64+
file_menu = tk.Menu(menubar)
65+
66+
menubar.add_cascade(menu=file_menu, label="File")
67+
68+
file_menu.add_command(label="New", command=create_file, accelerator="Ctrl+N")
69+
file_menu.add_command(label="Open...", command=open_file, accelerator="Ctrl+O")
70+
file_menu.add_command(label="Save", command=save_file, accelerator="Ctrl+S")
71+
72+
notebook = ttk.Notebook(main)
73+
74+
create_file()
75+
76+
root.bind("<Control-n>", lambda event: create_file())
77+
root.bind("<Control-o>", lambda event: open_file())
78+
root.bind("<Control-s>", lambda event: save_file())
79+
80+
root.mainloop()

0 commit comments

Comments
 (0)
0