-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
292 lines (250 loc) · 9.93 KB
/
main.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
"""
VocabiPy.
"""
# from ttkwidgets.autocomplete import AutocompleteEntry
import tkinter as tk
import json
import customtkinter as ctk
import pyperclip as clip
import dictionarymethods as dt
class App(ctk.CTk):
"""
This class places all the window's widgets of the app and defines
most of their behaviors.
"""
def __init__(self):
# --------------------Main root Window--------------------#
super().__init__()
self.geometry("500x400")
self.title("VocabiPy")
self.iconbitmap("mainicon.ico")
# --------------------Getting Settings-------------------#
# Trying to open the settings json file and get its data.
try:
with open("./settings.json", encoding="utf-8") as file:
file_settings = file.read()
# If the settings file doesn't exist, it will create a new one.
except FileNotFoundError:
# This is just a different way to make a long string
file_settings = (
"{"
+ '"selected_font": "Fixedsys,12", '
+ '"language": "english", '
+ '"appearance_mode":"System"'
+ "}"
)
with open("./settings.json", "w", encoding="utf-8") as file:
file.write(file_settings)
self.settings = json.loads(file_settings)
# This will call the selected_font.setter and process the string input.
self.selected_font = self.settings["selected_font"]
# --------------------Settings Frame--------------------#
# settings frame for app settings
self.settings_frame = ctk.CTkFrame(self, width=50)
self.settings_frame.pack(fill=tk.X, side=tk.TOP, padx=2, pady=2)
self.settings_frame.grid_columnconfigure(3, weight=1)
self.settings_frame.grid_rowconfigure(1, weight=1)
# --------------------Entry Frame--------------------#
# entry frame containing the query input and search button
self.entry_frame = ctk.CTkFrame(self, corner_radius=10)
self.entry_frame.pack(pady=20)
# --------------------Inside settings_frame--------------------#
# button for copying the meaning to clipboard
self.copy_meaning_button = ctk.CTkButton(
self.settings_frame,
text="Copy Meaning",
font=self.selected_font,
width=40,
command=self.copy_meaning,
)
self.copy_meaning_button.grid(row=0, column=1, padx=5, pady=5, sticky="nswe")
# change font option menu
self.change_font_button = ctk.CTkButton(
self.settings_frame,
text="Change Font",
font=self.selected_font,
command=self.change_font,
)
self.change_font_button.grid(row=0, column=2, padx=5, pady=5, sticky="nswe")
# "Workaround" so the first color value is the previously selected one.
color_values = ["Dark", "Light", "System"]
color_values.remove(self.settings["appearance_mode"])
color_values = [self.settings["appearance_mode"]] + color_values
# change theme option menu
self.theme_menu = ctk.CTkOptionMenu(
self.settings_frame,
values=color_values,
font=self.selected_font,
command=self.change_theme,
)
self.theme_menu.grid(row=0, column=3, padx=5, pady=5, sticky="nswe")
# --------------------Inside entry_frame--------------------#
# taking the query input
self.query_entry = ctk.CTkEntry(
self.entry_frame,
width=180,
border_width=1,
placeholder_text="Search word...",
font=self.selected_font,
)
self.query_entry.grid(row=0, column=0, pady=5, padx=5)
self.query_entry.bind("<Return>", self.search_meaning)
# preferred language
self.language_entry = ctk.CTkEntry(
self.entry_frame,
font=self.selected_font,
width=80,
placeholder_text="language",
)
self.language_entry.grid(row=0, column=1, pady=5, padx=5)
self.language_entry.insert(0, self.settings["language"])
# search for the meaning button
self.search_button = ctk.CTkButton(
self.entry_frame,
text="Search",
width=50,
font=self.selected_font,
command=self.search_meaning,
)
self.search_button.grid(row=0, column=2, pady=5, padx=5)
# --------------------TextBox for displaying the meanings--------------------#
self.search_results = ctk.CTkTextbox(
self,
font=self.selected_font,
)
self.search_results.pack(
fill=tk.BOTH,
expand=True,
pady=10,
padx=10,
ipadx=10,
ipady=10,
)
self.search_results.configure(state="disabled")
# Theme must only be changed after "search_results" is created.
self.change_theme(self.settings["appearance_mode"])
# running the app
self.mainloop()
@property
def selected_font(self):
"""
A property that returns the selected font.
"""
return self._selected_font
@selected_font.setter
def selected_font(self, param):
"""
Selected font's setter.
It can receive either a tuple or a string as input.
Default font size is 12. Minimum size is 6, maximum size is 25.
e.g.:
self.selected_font = ("Arial", 12)
self.selected_font = ("Arial",)
self.selected_font = "Arial,12"
self.selected_font = "Arial"
"""
if isinstance(param, tuple) and len(param) == 2:
if param[1] < 6:
param = (param[0], 6)
elif param[1] > 25:
param = (param[0], 25)
self._selected_font = param
return
if isinstance(param, tuple) and len(param) == 1:
self._selected_font = (param[0], 12)
if isinstance(param, str) and "," in param:
font, font_size = param.split(",")
font_size = int(font_size)
if font_size < 6:
font_size = 6
elif font_size > 25:
font_size = 25
self._selected_font = (font, font_size)
return
if isinstance(param, str) and "," not in param:
self._selected_font = (param, 12)
return
raise TypeError(
"Selected font should be a tuple or a string. Check docstring for more info."
)
# --------------------class App Attributes/Functions--------------------#
def search_meaning(self, event=None):
"""
This method inserts the searched meaning in the text box.
"""
language_selected = self.language_entry.get()
# Saving last language searched to file.
self.settings["language"] = language_selected
self.save_settings()
self.search_results.configure(state="normal")
self.search_results.delete("1.0", tk.END)
entry = self.query_entry.get()
meaning_list = dt.meaning(entry)
if len(meaning_list) != 1:
for meanings in meaning_list:
final_meaning = dt.translate(meanings, language_selected)
if final_meaning == "Couldn't translate that!":
self.search_results.insert(tk.END, final_meaning)
return
self.search_results.insert(tk.END, f"■ {final_meaning}\n\n")
else:
tips = (
"Possible causes for error :- \n"
+ " Check for Typos\n"
+ " The word might not be available in the API"
)
self.search_results.insert(tk.END, f"{meaning_list[0]}\n\n{tips}")
self.search_results.configure(state="disabled")
def change_theme(self, mode):
"""
Change theme command.
"""
ctk.set_appearance_mode(mode)
self.settings["appearance_mode"] = mode
self.save_settings()
def change_font(self):
"""
Change font command.
"""
# getting the name of the font
font_name_input_dialog = ctk.CTkInputDialog(
None,
text="Enter font name...",
title="Change Font",
)
if not (font := font_name_input_dialog.get_input()):
return
self.selected_font = font
# trying to change the font
try:
self.copy_meaning_button.configure(font=self.selected_font)
self.change_font_button.configure(font=self.selected_font)
self.theme_menu.configure(font=self.selected_font)
self.query_entry.configure(font=self.selected_font)
self.language_entry.configure(font=self.selected_font)
self.search_button.configure(font=self.selected_font)
self.search_results.configure(font=self.selected_font)
except tk.TclError:
error_message = "Couldn't change the font!"
self.search_results.configure(state="normal")
self.search_results.insert(tk.END, error_message)
self.search_results.configure(state="disabled")
# Saving font to file. This way of saving allows the user to input the font
# size as well. e.g. write Arial,20 in the input dialog.
self.settings["selected_font"] = "{},{}".format(*self.selected_font)
self.save_settings()
def copy_meaning(self):
"""
This method copies the meaning in the textbox into the user's
clipboard.
"""
meaning = self.search_results.get("1.0", "end")
clip.copy(meaning)
def save_settings(self):
"""
This function saves the current state of the settings into
the settings.json file.
"""
with open("./settings.json", "w", encoding="utf-8") as file:
file.write(json.dumps(self.settings))
app = App()