-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
302 lines (249 loc) · 11.9 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
293
294
295
296
297
298
299
300
301
302
import funcData
import PySimpleGUI as sg
import json, random
#----------------------------------------------------------------------------------------------------------------
#Global Variables:
#----------------------------------------------------------------------------------------------------------------
#UI elements:
window_width = 800
window_height = 600
header_size = 32
button_size = 18
quiz_header_size = 18
question_header = 16
flashcard_question = 30
flashcard_question_small = 20
score_size = 24
expanded_score_size = 24
urb = 'Urbane Rounded Bold'
urm = 'Urbane Rounded Medium'
PolishTheme = {
'BACKGROUND': '#FFFFFF',
'TEXT': '#998675',
'INPUT': '#F7F7F7',
'TEXT_INPUT': '#404040',
'SCROLL': '#99CC99',
'BUTTON': ('#66605D', '#CEC8C2'), #Text, then background
'PROGRESS': ('#D1826B', '#CC8019'),
'BORDER': 1, 'SLIDER_DEPTH': 0,
'PROGRESS_DEPTH': 0,
}
#Program Variables
#quizSize = 5
#----------------------------------------------------------------------------------------------------------------
#Making Window Functions (Neccessary as functions since layouts cannot be re-used.)
#----------------------------------------------------------------------------------------------------------------
def makeFlashcardWindow():
flashcardLayout = [
#Score
[sg.Button('<', key='-BACK-', size=(4, 1), font=(urm, button_size)), sg.Text('', key='-EXPAND3-', pad=(0,0)), sg.Text('0/0', key="-SCORE-", font=(urb, score_size), text_color='#000000', justification='right')],
#Correct/Incorrect desc + Correct Answer text
[sg.Text('', key='-FLASHCARD_RESULT-', font=(urm, quiz_header_size))],
[sg.Text('', key='-CORRECT_ANSWER_TEXT-', font=(urb, quiz_header_size), text_color='#000000')],
#Flashcard frame + Flashcard question + Filler(EXPAND)
[sg.Frame('', [
[sg.Text(key='-EXPAND-', pad=(0,0), background_color='#F7F7F7')],
[sg.Text('', key='-FLASHCARD_QUESTION-', font=(urb, flashcard_question), background_color='#F7F7F7')],
[sg.Text(key='-EXPAND2-', pad=(0,0), background_color='#F7F7F7')],
], size = (500,350), element_justification='c', vertical_alignment='center', background_color='#F7F7F7')
],
#Answer Input + Submit button
[sg.In(key='-FLASHCARD_ANSWER-', size=(28,2), font=(urm, question_header), enable_events=True), sg.Button(">", key='-SKIP-', size=(4, 1), font=(urm, button_size))],
[sg.Button('Submit', key='-SUBMIT-', size=(8, 1), font=(urm, button_size))]
]
return sg.Window("Flashcards", flashcardLayout, finalize=True, size=(window_width, window_height), element_justification='c')
def makeMainWindow():
mainMenuLayout = [
[sg.Text('', font=(urb, 45))],
[sg.Text("Custom Polish Quiz Generator", font=(urb, header_size))],
[sg.Text("", font=(urb, header_size))],
#[sg.Button('Start Quiz')],
[sg.Button("Flashcards", size=(10, 2),font=(urm, button_size))],
# [sg.Button("Add Question Data")],
[sg.Button("Quit", size=(10, 2),font=(urm, button_size))]
]
return sg.Window("Polish_Quiz", mainMenuLayout, size=(window_width, window_height), element_justification='c')
def makeQuizSelectWindow():
layout = [
[sg.Text("What would you like to practice? ", font=(urm, header_size))],
[sg.Text('')],
[sg.Button('Adjectives', key='-PRACTICE_ADJECTIVES-', font=(urm, button_size), size=(10, 2))],
[sg.Button('Nouns', key='-PRACTICE_NOUNS-', font=(urm, button_size), size=(10, 2))],
[sg.Button('Verbs', key='-PRACTICE_VERBS-', font=(urm, button_size), size=(10, 2))],
[sg.Button('All', key='-PRACTICE_ALL-', font=(urm, button_size), size=(10, 2))],
]
return sg.Window("Flashcard Quiz Select", layout, element_justification='c', size=(window_width, window_height))
def makeScoreWindow():
layout = [
[sg.Button('<', key='-BACK-', size=(4, 1), font=(urm, button_size)), sg.Text('', key='-EXPAND-', pad=(0,0))],
[sg.Text('')],
[sg.Frame('', [
[sg.Text('Final Score:', font=(urm, header_size),background_color='#F7F7F7'), sg.Text('', key='-FINAL_SCORE-', font=(urb, header_size),background_color='#F7F7F7')],
[sg.Text('', key='-SCORE_PERCENTAGE-', font=(urb, 40),background_color='#F7F7F7')],
[sg.Text('')],
[sg.Text('', key='-SCORE_MESSAGE-', font=(urm, 20),background_color='#F7F7F7')]
], size = (500,350), element_justification='c', vertical_alignment='center', background_color='#F7F7F7')
],
]
return sg.Window("Results", layout, element_justification='c', size=(window_width, window_height), finalize=True)
#----------------------------------------------------------------------------------------------------------------
#Window Functionality
#----------------------------------------------------------------------------------------------------------------
def flashcardWindow(data, setType):
#print("Entered flashcard window")
window = makeFlashcardWindow()
window['-EXPAND-'].expand(True, True, True)
window['-EXPAND2-'].expand(True, False, True)
window['-EXPAND3-'].expand(True, False, False)
window.bind("<Return>", "_Enter")
window.bind("<Tab>", "_Tab")
print(type(data), len(data))
if (setType == 'all'):
dataSet = {}
dataSet.update(data["adjective_set"])
dataSet.update(data["noun_set"])
dataSet.update(data["verb_set"])
print(dataSet)
else:
dataSet = data[setType]
#Prevent looping
usedTerms = {}
score = 0
questionCount = 0
event, values = '', ''
while True:
window['-SCORE-'].update(str(score)+'/' + str(questionCount))
#----------------------
#Code to stop the quiz at a certain quiz size. Quiz size global variable available above
# print(questionCount, quizSize, window['-SCORE-'].get())
# if (questionCount == quizSize):
# window.close()
# scoreWindow(data, window['-SCORE-'].get())
# break
if (event != '-FLASHCARD_ANSWER-'):
while True:
if len(usedTerms) == len(dataSet):
window.close()
scoreWindow(data, window['-SCORE-'].get())
break
question, answer = funcData.setupFlashcard(dataSet)
if (question in usedTerms) == False:
usedTerms[question] = answer
break
#print(usedTerms)
#Randomizing whether the question is polish->english or english->polish
window['-FLASHCARD_QUESTION-'].update(font=(urm, flashcard_question))
if (random.randint(0,1) == 0):
if type(answer) is list:
newQuestion = ''
for i in range(len(answer)):
newQuestion += answer[i]
if (i != len(answer)-1):
newQuestion += " or "
window['-FLASHCARD_QUESTION-'].update(font=(urm, flashcard_question_small))
else:
newQuestion = answer
answer = question
question = newQuestion
window['-FLASHCARD_ANSWER-'].update('')
#Populating/Updating the layout with the current question
window['-FLASHCARD_QUESTION-'].update(question)
#---------------------------------
event, values = window.read()
#print(event, values)
if event == sg.WIN_CLOSED or event == 'Quit':
window.close()
break
elif event == '-BACK-':
#add popup here
window.close()
mainMenuWindow(data)
elif event == '-SUBMIT-' or event == '_Enter' or event == '-SKIP-' or event == '_Tab':
#print(values['-FLASHCARD_ANSWER-'])
if event == "-SKIP-" or event == '_Tab':
checkResult, resultDescription, correctAnswer = (-1), 'Question skipped.', ''
checkResult, resultDescription, correctAnswer = funcData.checkFlashcardAnswer(answer, 'skip')
else:
checkResult, resultDescription, correctAnswer = funcData.checkFlashcardAnswer(answer,values['-FLASHCARD_ANSWER-'])
if (checkResult == 1):
score+=1
window['-FLASHCARD_RESULT-'].update(text_color='#3EFF00')
window['-CORRECT_ANSWER_TEXT-'].update(correctAnswer)
elif checkResult == (-1):
print('skipped')
window['-FLASHCARD_RESULT-'].update(text_color='#000000')
window['-CORRECT_ANSWER_TEXT-'].update(correctAnswer)
questionCount-=1
else:
#print('wrong' + str(correctAnswer))
window['-FLASHCARD_RESULT-'].update(text_color='#FF0000')
window['-CORRECT_ANSWER_TEXT-'].update(correctAnswer)
questionCount+=1
window['-FLASHCARD_RESULT-'].update(resultDescription)
def scoreWindow(data, score):
window = makeScoreWindow()
window['-EXPAND-'].expand(True, False, False)
window['-FINAL_SCORE-'].update(score)
scoreValues = score.split('/')
scorePercent = round((int(scoreValues[0]) / int(scoreValues[1])) * 100 )
#print("score%" +str(scorePercent))
window['-SCORE_PERCENTAGE-'].update(str(scorePercent)+ '%')
#Changes dependent on score thresholds:
if (scorePercent >= 80):
window['-SCORE_PERCENTAGE-'].update(text_color='#3EFF00')
window['-SCORE_MESSAGE-'].update("Great work! You're a genius!")
elif (scorePercent >= 60):
window['-SCORE_PERCENTAGE-'].update(text_color='#FFE883')
window['-SCORE_MESSAGE-'].update("Good job, keep practicing!")
else:
window['-SCORE_MESSAGE-'].update("I love you, keep working hard!")
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == '':
window.close()
break
elif event == '-BACK-':
window.close()
mainMenuWindow(data)
def mainMenuWindow(data):
#print("Entered main window")
window = makeMainWindow()
while True:
event, values = window.read()
#print(event, values)
if event == sg.WIN_CLOSED or event == 'Quit':
window.close()
break
elif event == 'Flashcards':
window.close()
quizSelectWindow(data)
def quizSelectWindow(data):
window = makeQuizSelectWindow()
while True:
event, values = window.read()
#print(event, values)
if event == sg.WIN_CLOSED or event == '':
window.close()
break
elif event == '-PRACTICE_ADJECTIVES-':
window.close()
flashcardWindow(data, 'adjective_set')
elif event == '-PRACTICE_NOUNS-':
window.close()
flashcardWindow(data, 'noun_set')
elif event == '-PRACTICE_VERBS-':
window.close()
flashcardWindow(data, 'verb_set')
elif event == '-PRACTICE_ALL-':
window.close()
flashcardWindow(data, 'all')
def main():
sg.LOOK_AND_FEEL_TABLE['PolishTheme'] = PolishTheme
sg.theme('PolishTheme')
with open('data.json', 'r', encoding='utf-8') as json_file:
#data = json.dumps(json.load(json_file), ensure_ascii=False)
data = json.load(json_file)
#print(data)
#print("Entered main")
mainMenuWindow(data)
main()