-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfontchooser.py
76 lines (55 loc) · 1.88 KB
/
fontchooser.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
import tkinter as tk
import tkinter.font
class FontChooser(tk.Toplevel):
"""
A font chooser dialog. This will create the dialog and show it.
:param master: The root window.
"""
def __init__(self, master=None):
"""
Initialize the FontChooser class.
"""
super().__init__(master)
self.title("Choose a font")
self.fonts = list(tkinter.font.families())
self.fonts.sort()
self.listbox = tk.Listbox(self)
self.listbox.pack(fill=tk.BOTH, expand=1)
self.listbox.config(font=("Arial", 12))
for f in self.fonts:
self.listbox.insert(tk.END, f)
self.listbox.bind("<<ListboxSelect>>", self.on_select)
button_frame = tk.Frame(self)
button_frame.pack(fill=tk.X, side=tk.BOTTOM)
ok_button = tk.Button(
button_frame, text="OK", command=self.ok, font=("Arial", 16)
)
ok_button.pack(side=tk.RIGHT)
ok_button.config(width=4, height=2)
cancel_button = tk.Button(
button_frame, text="Cancel", command=self.cancel, font=("Arial", 16)
)
cancel_button.pack(side=tk.RIGHT)
cancel_button.config(width=4, height=2)
self.selected_font = None
def on_select(self, event):
index = self.listbox.curselection()[0]
self.selected_font = self.listbox.get(index)
def ok(self):
self.destroy()
def cancel(self):
self.selected_font = None
self.destroy()
def get_font(self):
return self.selected_font
# root = tk.Tk()
# font_chooser = FontChooser(root)
# root.wait_window(
# font_chooser
# ) # this makes the parent window wait until the FontChooser window is closed
# selected_font = font_chooser.get_font()
# if selected_font:
# print("Selected font:", selected_font)
# else:
# print("No font selected")
# root.mainloop()