-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
executable file
·102 lines (80 loc) · 2.49 KB
/
app.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
#!/usr/bin/python3
from tkinter import *
from models.db import dbModel
from models.matches import get_matches
db = dbModel()
root = Tk()
def _search():
"""
Searches for a word in the database.
Returns:\n
str: The meanings of the word if found,
a suggestion if the word is not found,
or "No records found." if the word is not found.
"""
_text = keyword_text.get().lower()
try:
responses = db.get_meaning(_text)[0]
except IndexError:
responses = None
if responses is None:
try:
suggestion = get_matches(_text, db.get_all(_text))
if suggestion:
return f"Do you mean '{suggestion}'?"
except IndexError:
return "No records found."
else:
meanings = responses[1].split('", ')
return meanings
def search_command():
"""
Searches for specific keyword.
"""
display.delete(0, END)
meanings = _search()
if meanings == "No records found.":
display.insert(END, meanings)
return
if str(meanings).startswith("Do you mean"):
display.insert(END, meanings)
return
for i in range(len(meanings)):
meaning = "{}. {}\n".format(i+1,
meanings[i].strip("[]").strip('"'))
display.insert(END, meaning)
def close_command():
"""
Closes database and exit the window close button is toggled
"""
db.close_db()
root.destroy()
def clear_command():
"""
Clears the buffer (entry and listbox)
"""
keyword_entry.delete(0, END)
display.delete(0, END)
root.wm_title("EmyDictionary")
# Labels
keyword = Label(root, text="Keyword")
keyword.grid(row=1, column=0)
meaning = Label(root, text="Meaning")
meaning.grid(row=2, column=0)
# Entry
keyword_text = StringVar()
keyword_entry = Entry(root, border=2, textvariable=keyword_text)
keyword_entry.grid(row=1, column=1)
# Buttons
search = Button(root, text="Search", width=12, border=3, command=search_command)
search.grid(row=2, column=9)
clear = Button(root, text="Clear", width=12, border=3, command=clear_command)
clear.grid(row=4, column=9)
close = Button(root, text="Close", width=12, border=3, command=close_command)
close.grid(row=6, column=9)
# ListBox
display = Listbox(root, height=6, width=35, border=2)
display.grid(row=2, column=1, columnspan=3, rowspan=5)
__copyright = Label(root, text="(C) EmyCodes 2024")
__copyright.grid(row=8, column=1, columnspan=3, rowspan=5)
root.mainloop()