Skip to content

Commit dab2043

Browse files
committed
grid pack align
1 parent bb39674 commit dab2043

File tree

5 files changed

+134
-0
lines changed

5 files changed

+134
-0
lines changed

tkinter/grid-pack-align/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# align_grid.py
2+
3+
![#1](imgs/python-tkinter-grid.png?raw=true)
4+
5+
# align_pack.py
6+
7+
![#2](imgs/python-tkinter-pack.png?raw=true)
8+
9+

tkinter/grid-pack-align/align_grid.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python3
2+
3+
import tkinter as tk
4+
5+
# --- init ---
6+
7+
root = tk.Tk()
8+
root.title('Grid')
9+
10+
# --- text ---
11+
12+
text = '\n[very, very, very, very, very long text for example]'
13+
14+
# --- info ---
15+
16+
tk.Label(root, text="Bartłomiej 'furas' Burek (blog.furas.pl)", bg='black', fg='white', width=70).grid()
17+
tk.Label(root, text="import tkinter as tk", bg='black', fg='yellow', width=70).grid()
18+
19+
# --- align button/label/widget in cell ---
20+
21+
tk.Label(root, text="\n--------- align button/label/widget in cell ---------", bg='grey', fg='yellow', width=70).grid()
22+
tk.Label(root, text="use in grid(...)\n", bg='grey', fg='white', width=70).grid()
23+
24+
# ---
25+
26+
tk.Button(root, text="\n (nothing) \n"+text).grid()
27+
tk.Button(root, text="\n sticky='w' (or) sticky=tk.W \n"+text).grid(sticky='w')
28+
tk.Button(root, text="\n sticky='e' (or) sticky=tk.E \n"+text).grid(sticky='e')
29+
tk.Button(root, text="\n sticky='we' (or) sticky=tk.W+tk.E \n"+text).grid(sticky='we')
30+
31+
# --- align text in button/label ---
32+
33+
tk.Label(root, text="\n--------- align text in button/label ---------", bg='grey', fg='yellow', width=70).grid()
34+
tk.Label(root, text="use in Label(...)/Button(...)\n", bg='grey', fg='white', width=70).grid()
35+
tk.Label(root, text="grid( sticky='we' (or) sticky=tk.W+tk.E )", bg='gray', width=70).grid()
36+
37+
# ---
38+
39+
tk.Button(root, text="\n (nothing) \n"+text).grid(sticky='we')
40+
tk.Button(root, text="\n anchor='w' (or) anchor=tk.W \n"+text, anchor='w').grid(sticky='we')
41+
tk.Button(root, text="\n anchor='e' (or) anchor=tk.E \n"+text, anchor='e').grid(sticky='we')
42+
43+
# --- align lines in text in button/label ---
44+
45+
tk.Label(root, text="\n--------- align lines in text in button/label ---------", bg='grey', fg='yellow', width=70).grid()
46+
tk.Label(root, text="use in Label(...)/Button(...)\n", bg='grey', fg='white', width=70).grid()
47+
tk.Label(root, text="grid( sticky='we' (or) sticky=tk.W+tk.E )", bg='gray', width=70).grid()
48+
49+
# ---
50+
51+
tk.Button(root, text="\n (nothing)\n"+text).grid(sticky='we')
52+
tk.Button(root, text="\n justify='left' (or) justify=tk.LEFT \n"+text, justify='left').grid(sticky='we')
53+
tk.Button(root, text="\n justify='right' (or) justify=tk.RIGHT \n"+text, justify='right').grid(sticky='we')
54+
tk.Button(root, text="\n justify='center' (or) justify=tk.CENTER \n"+text, justify='center').grid(sticky='we')
55+
56+
# --- info ---
57+
58+
tk.Label(root, text="w = west/left , e = east/right , n = north/top , s = south/bottom", bg='black', fg='white', width=70).grid()
59+
60+
# --- "start the engine" ---
61+
62+
root.mainloop()

tkinter/grid-pack-align/align_pack.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env python3
2+
3+
import tkinter as tk
4+
5+
# --- init ---
6+
7+
root = tk.Tk()
8+
root.title('Pack')
9+
10+
# --- text ---
11+
12+
text = '\n[very, very, very, very, very long text for example]'
13+
14+
# --- info ---
15+
16+
tk.Label(root, text="Bartłomiej 'furas' Burek (blog.furas.pl)", bg='black', fg='white', width=70).pack()
17+
tk.Label(root, text="import tkinter as tk", bg='black', fg='yellow', width=70).pack()
18+
19+
# --- align button/label/widget in cell ---
20+
21+
tk.Label(root, text="\n--------- align button/label/widget in cell ---------", bg='grey', fg='yellow', width=70).pack()
22+
tk.Label(root, text="use in pack(...)\n", bg='grey', fg='white', width=70).pack()
23+
24+
# ---
25+
26+
tk.Button(root, text="\n (nothing) \n"+text).pack()
27+
tk.Button(root, text="\n anchor='w' (or) anchor=tk.W \n"+text).pack(anchor='w')
28+
tk.Button(root, text="\n anchor='e' (or) anchor=tk.E \n"+text).pack(anchor='e')
29+
#tk.Label(root, text="\nCAN'T DO: anchor='we' (or) anchor=tk.W+tk.E\n", fg='red').pack()
30+
tk.Button(root, text="\n fill='x' (or) fill=tk.X \n"+text).pack(fill='x')
31+
32+
# --- align text in button/label ---
33+
34+
tk.Label(root, text="\n--------- align text in button/label ---------", bg='grey', fg='yellow', width=70).pack()
35+
tk.Label(root, text="use in Label(...)/Button(...)\n", bg='grey', fg='white', width=70).pack()
36+
tk.Label(root, text="pack( fill='x' (or) fill=tk.X )", bg='gray', width=70).pack()
37+
38+
# ---
39+
40+
tk.Button(root, text="\n (nothing) \n"+text).pack(fill='x')
41+
tk.Button(root, text="\n anchor='w' (or) anchor=tk.W \n"+text, anchor='w').pack(fill='x')
42+
tk.Button(root, text="\n anchor='e' (or) anchor=tk.E \n"+text, anchor='e').pack(fill='x')
43+
44+
# --- align lines in text in button/label ---
45+
46+
tk.Label(root, text="\n--------- align lines in text in button/label ---------", bg='grey', fg='yellow', width=70).pack()
47+
tk.Label(root, text="use in Label(...)/Button(...)\n", bg='grey', fg='white', width=70).pack()
48+
tk.Label(root, text="pack( fill='x' (or) fill=tk.X )", bg='gray', width=70).pack()
49+
50+
# ---
51+
52+
tk.Button(root, text="\n (nothing) \n"+text).pack(fill='x')
53+
tk.Button(root, text="\n justify='left' (or) justify=tk.LEFT \n"+text, justify='left').pack(fill='x')
54+
tk.Button(root, text="\n justify='right' (or) justify=tk.RIGHT \n"+text, justify='right').pack(fill='x')
55+
tk.Button(root, text="\n justify='center' (or) justify=tk.CENTER \n"+text, justify='center').pack(fill='x')
56+
57+
# --- info ---
58+
59+
tk.Label(root, text="w = west/left , e = east/right , n = north/top , s = south/bottom", bg='black', fg='white', width=70).pack()
60+
61+
# --- "start the engine" ---
62+
63+
root.mainloop()
Loading
Loading

0 commit comments

Comments
 (0)