-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
140 lines (109 loc) · 4.56 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
import PySimpleGUI as sg
import json
# reads the words_dictionary.json
with open('words_dictionary.json') as file:
data = json.load(file)
left_layout = [[(sg.Text('English Words Finder\n', font=('', 25, 'bold')))],
[sg.Text('Words by Length:', font=('', 10, 'bold')),
(sg.Combo(values=[num for num in range(0, 26)], default_value="0", key='LENGTH', enable_events=True))],
[(sg.Text('Word:', font=('', 10, 'bold'))),
(sg.Input(key='INPUT', size=(25, 1), justification='center', enable_events=True))]]
right_layout = [[(sg.Listbox(values=[], enable_events=True, size=(25, 15), key='WORDS'))],
[(sg.Button('<', key='PREVIOUS')), (sg.Button('>', key='NEXT'))]]
layout = [[sg.Column(left_layout, element_justification='center'), sg.Column(right_layout, element_justification='center')]]
window = sg.Window('English Words Finder', layout, size=(600, 310), element_justification='c', finalize=True)
window.set_icon('icon.ico')
words_list = []
move = False
set = 1
# function for clearing the list
def clear_list():
words_list.clear()
window['WORDS'].update(values=words_list)
# function when the length is changed
def words_length():
if values['LENGTH'] == 0: # if length is 0
if values['INPUT'] == '': # if input is empty and chose 0 then print all words
for word in data.keys():
words_list.append(word)
else: # if input is not empty and chose 0 then print words that starts with the input
for word in data.keys():
if word.startswith(values['INPUT']):
words_list.append(word)
else: # if length is not 0 then print words that starts with the input with equivalent length
for word in data.keys():
if word.startswith(values['INPUT']) and len(word) == values['LENGTH']:
words_list.append(word)
else:
pass
# function for searching the words
def words_search():
if not values['INPUT'] == '': # if input is not empty
if not values['LENGTH'] == 0: # if length is not 0 then print words that starts with the input with equivalent length
for word in data.keys():
if word.startswith(values['INPUT']) and len(word) == values['LENGTH']:
words_list.append(word)
else:
pass
else: # if length is 0 then print words that starts with the input
for word in data.keys():
if word.startswith(values['INPUT']):
words_list.append(word)
else: # if input is empty
if values['LENGTH'] == 0: # if length is 0 then print all words
for word in data.keys():
words_list.append(word)
else: # if length is not 0 then print words that is equivalent to length
for word in data.keys():
if word.startswith(values['INPUT']) and len(word) == values['LENGTH']:
words_list.append(word)
# function when previous or next is clicked
def previous_and_next(sign, totalset):
global set
if sign == '<': # if previous
if not set == 1:
set -= 1
window['WORDS'].update(values=words_list[(set * 100) - 100:set * 100])
else:
pass
else: # if next
if set < totalset:
set += 1
window['WORDS'].update(values=words_list[(set * 100) - 100:set * 100])
else:
pass
# function for checking the set of words
def check_set():
length = len(words_list)
quotient = length / 100
remainder = length % 100
if not remainder == 0:
remainder = 1
else:
pass
return int(quotient + remainder)
while True:
move = False
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
elif event == 'LENGTH': # if length is changed
clear_list()
words_length()
set = 1
elif event == 'INPUT': # if search button is clicked
clear_list()
words_search()
set = 1
elif event == 'PREVIOUS': # if previous button is clicked
previous_and_next('<', check_set())
move = True
elif event == 'NEXT': # if next button is clicked
previous_and_next('>', check_set())
move = True
if not move:
if len(words_list) <= 100: # if words below 100
window['WORDS'].update(values=words_list)
else: # if words greater than 100 then only prints the first 100
window['WORDS'].update(values=words_list[:100])
window.close()