forked from kr-g/pytkfaicons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.py
244 lines (168 loc) · 6.35 KB
/
sample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import tkinter as tk
from PIL import Image, ImageTk, ImageDraw
if False:
# set to True to build all icons
from pytkfaicons.conv import build, get_repo, convert_all, copy_meta, copy_fonts
# build(opts="-c", callb=print) # use custom credit file
# build(callb=print) # use global .pygg_credits file
# convert_all()
# copy_meta()
# copy_fonts()
from pytkfaicons.conv import get_meta, get_scaled_tk_icon, get_colored_scaled_tk_icon
from pytkfaicons.icons import get_icon, get_icon_image, tk_image_loader, get_tk_icon
from pytkfaicons.fonts import get_font, get_font_icon
root = tk.Tk()
frame_t = tk.Frame(root)
tk.Label(frame_t, text="svg based - different size and color support").pack(
side="left", pady=20
)
frame_t.pack()
frame1 = tk.Frame(root)
tk.Label(frame1, text="on the fly resized (slow)").pack(side="left")
# resize on the fly (slow)
img0 = get_colored_scaled_tk_icon("arrow-left", "solid", 64, "green")
tk.Button(frame1, justify=tk.LEFT, padx=10, image=img0).pack(side="left")
img1 = get_colored_scaled_tk_icon("arrow-right", "solid", 48, "green", invert=True)
tk.Button(frame1, justify=tk.LEFT, padx=10, image=img1).pack(side="left")
img2 = get_colored_scaled_tk_icon("asterisk", "solid", 32, "blue", invert=True)
tk.Button(frame1, justify=tk.LEFT, padx=10, image=img2).pack(side="left")
img3 = get_colored_scaled_tk_icon("barcode", "solid", 24, "blue")
tk.Button(frame1, justify=tk.LEFT, padx=10, image=img3).pack(side="left")
img4 = get_scaled_tk_icon("baby", "solid", 12)
tk.Button(frame1, justify=tk.LEFT, padx=10, image=img4).pack(side="left")
img5 = get_scaled_tk_icon("bahai", "solid", 8)
tk.Button(frame1, justify=tk.LEFT, padx=10, image=img5).pack(side="left")
img6 = get_scaled_tk_icon("bars", "solid", 6)
tk.Button(frame1, justify=tk.LEFT, padx=10, image=img6).pack(side="left")
frame1.pack()
# this is the same implementation as get_tk_icon()
# using get_icon_image(), and tk_image_loader() as loader
# what returns tk.PhotoImage
def icon(name, style):
# pre-calculated 32px height icons (fast)
return get_icon_image(name, style, loader=tk_image_loader)
frame2 = tk.Frame(root)
left = icon("arrow-left", "solid")
right = icon("arrow-right", "solid")
asterisk = icon("asterisk", "solid")
barcode = icon("barcode", "solid")
baby = icon("baby", "solid")
bahai = icon("bahai", "solid")
bars = icon("bars", "solid")
tk.Label(frame2, text="pre-caclulated (fast)").pack(side="left")
tk.Button(frame2, justify=tk.LEFT, padx=10, image=left).pack(side="left")
tk.Button(frame2, justify=tk.LEFT, padx=10, image=right).pack(side="left")
tk.Button(frame2, justify=tk.LEFT, padx=10, image=asterisk).pack(side="left")
tk.Button(frame2, justify=tk.LEFT, padx=10, image=barcode).pack(side="left")
tk.Button(frame2, justify=tk.LEFT, padx=10, image=baby).pack(side="left")
tk.Button(frame2, justify=tk.LEFT, padx=10, image=bahai).pack(side="left")
tk.Button(frame2, justify=tk.LEFT, padx=10, image=bars).pack(side="left")
frame2.pack()
# info text
frame_m = tk.Frame(root)
tk.Label(frame_m, text="font based - different size and color support").pack(
side="left", pady=20
)
frame_m.pack()
#
# use fonts
f_brands = get_font("brands", 44)
print(f_brands.getname())
f_solid = get_font("solid", 44)
print(f_solid.getname())
f_regular = get_font("regular", 44)
print(f_regular.getname())
# custom image - thats how get_font_icon() is implemented
meta_ic = get_meta("asterisk")
unicode_ic = meta_ic["unicode"] # this is a hex string
style_ic = meta_ic["styles"] # list of supported fonts
print("icon-data", unicode_ic, style_ic)
print("icon-meta", meta_ic)
# convert hex string to unicode
unicode_text = chr(int(unicode_ic, 16))
# load the first supported font
font_ic = get_font(style_ic[0], 44)
# background color == green
imag = Image.new(mode="RGB", size=(66, 66), color="black")
draw = ImageDraw.Draw(im=imag)
# draw text with color == white
draw.text(xy=(33, 33), text=unicode_text, font=font_ic, fill="white", anchor="mm")
im = ImageTk.PhotoImage(imag)
# end-of custom image
frame3 = tk.Frame(root)
tk.Label(frame3, text="font based").pack(side="left")
# use the custom image
tk.Button(frame3, justify=tk.LEFT, padx=10, image=im).pack(side="left")
im_python = get_font_icon("python", height=100, bg="green", fg="white")
tk.Button(frame3, justify=tk.LEFT, padx=10, image=im_python).pack(side="left")
im_baby = get_font_icon("baby", height=44, bg="blue", fg="white")
tk.Button(frame3, justify=tk.LEFT, padx=10, image=im_baby).pack(side="left")
im_bahai = get_font_icon("bahai", height=44, image_size=(100, 70), bg="red", fg="white")
tk.Button(frame3, justify=tk.LEFT, padx=10, image=im_bahai).pack(side="left")
frame3.pack()
frame4 = tk.Frame(root)
tk.Label(frame4, text="font styles").pack(side="left")
im_quest = get_font_icon(
"question-circle",
style="regular",
height=22,
image_size=(50, 50),
bg="black",
fg="white",
)
tk.Button(frame4, justify=tk.LEFT, padx=10, image=im_quest).pack(side="left")
im_quest2 = get_font_icon(
"question-circle",
style="solid",
height=22,
image_size=(50, 50),
bg="black",
fg="white",
)
tk.Button(frame4, justify=tk.LEFT, padx=10, image=im_quest2).pack(side="left")
im_quest3 = get_font_icon(
"question-circle",
style="regular",
height=22,
image_size=(50, 50),
)
tk.Button(frame4, justify=tk.LEFT, padx=10, image=im_quest3).pack(side="left")
im_quest4 = get_font_icon(
"question-circle",
style="solid",
height=22,
image_size=(50, 50),
)
tk.Button(frame4, justify=tk.LEFT, padx=10, image=im_quest4).pack(side="left")
frame4.pack()
frame5 = tk.Frame(root)
tk.Label(frame5, text="coding icons").pack(side="left", pady=20)
frame5.pack()
frame6 = tk.Frame(root)
im_code = get_font_icon(
"code",
height=22,
image_size=(33, 33),
)
tk.Button(frame6, justify=tk.LEFT, padx=10, image=im_code).pack(side="left")
im_code2 = get_font_icon(
"bug",
height=22,
image_size=(33, 33),
)
tk.Button(frame6, justify=tk.LEFT, padx=10, image=im_code2).pack(side="left")
im_code_branch = get_font_icon(
"code-branch",
height=22,
image_size=(33, 33),
)
tk.Button(frame6, justify=tk.LEFT, padx=10, image=im_code_branch).pack(side="left")
im_code_cmp = get_font_icon(
"cube",
height=22,
image_size=(33, 33),
)
tk.Button(frame6, justify=tk.LEFT, padx=10, image=im_code_cmp).pack(side="left")
frame6.pack()
# run tkinter mainloop
root.mainloop()