-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlearner.py
207 lines (157 loc) · 8.24 KB
/
learner.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
#!/usr/bin/python
from tkinter import *
import tkinter as tk
import leitner
import flashcard
import database_manip as db
class Application(tk.Frame):
def __init__(self, master = None):
super().__init__(master)
self.master = master
self.window_setup()
print('App started.')
self.create_widgets()
leitner.initialize()
print('Database loaded.')
# leitner.tester_function(self)
self.mainloop()
def window_setup(self):
w = 600
h = 400
ws = self.master.winfo_screenwidth()
hs = self.master.winfo_screenheight()
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
self.place(relx = 0.5, rely = 0.5)
self.master.geometry('%dx%d+%d+%d' % (w, h, x, y))
self.pack(fill = BOTH, expand = 1)
def create_widgets(self):
quitButton = tk.Button(self, text = 'Close')
# quitButton.pack()
quitButton.bind('<ButtonRelease-1>', self.quit)
quitButton.bind('<KeyRelease-Return>', self.quit)
quitButton.place(relx=0.5, rely=0.75, anchor=CENTER)
startSessionButton = tk.Button(self, text = 'New session')
startSessionButton.bind('<ButtonRelease-1>', self.start_a_session)
startSessionButton.bind('<KeyRelease-Return>', self.start_a_session)
startSessionButton.place(relx = 0.5, rely = 0.25, anchor = CENTER)
insertButton = tk.Button(self, text = 'Insert new word')
insertButton.bind('<ButtonRelease-1>', self.create_insertion_popup)
insertButton.bind('<KeyRelease-Return>', self.create_insertion_popup)
insertButton.place(relx = 0.5, rely = 0.5, anchor = CENTER)
print('Widgets created.')
def quit(self, event):
print('Quitting...')
self.master.destroy()
#start a session
def start_a_session(self, event):#event
self.currentSession = leitner.db.get_nextSession()
print('Session started (number',self.currentSession,').')
#gather cards
self.cardsForQuery = []
for card in leitner.deckCurrent.get_flashcards():
self.cardsForQuery.append(card)
#needs editing
for deckIndex in leitner.get_session_deck_indices(self.currentSession):
deck = leitner.progressDeck[ deckIndex ]
for card in deck.get_flashcards():
self.cardsForQuery.append(card)
self.queryPopup = tk.Toplevel(self, width = 300, height = 200, takefocus = True)
self.labelWidget = tk.Label(self.queryPopup)
self.entryWidget = tk.Entry(self.queryPopup)
closeButton = tk.Button(self.queryPopup, text = 'Check')
closeButton.bind('<ButtonRelease-1>', self.submit_guess)
closeButton.bind('<KeyRelease-Return>', self.submit_guess)
closeButton.place(relx = 0.5, rely = 1, y = -10, anchor = S)
self.display_next_query()
def display_next_query(self):
if len(self.cardsForQuery) == 0:
#increment nextSession (also in file)
self.quit_session(None)
else:
self.currentCard = self.cardsForQuery.pop()
self.labelWidget.destroy()
self.labelWidget = tk.Label(self.queryPopup, text = 'Type in the translation:\n'
+ self.currentCard.word)
self.labelWidget.place(width = 275, height = 50, relx = 0.5, rely = 0, y = 10, anchor = N)
self.entryWidget.destroy()
self.entryWidget = tk.Entry(self.queryPopup)
self.entryWidget.place(width = 250, height = 25, relx = 0.5, y = 70, anchor = N)
self.entryWidget.bind('<KeyRelease-Return>', self.submit_guess)
self.entryWidget.focus()
#check for correctness
#move on to next query
def submit_guess(self, event):
submittedGuess = self.entryWidget.get()
correct = (submittedGuess == self.currentCard.translation)
if correct:
if self.currentCard.deckNo == -1:#current
leitner.deckCurrent.remove(self.currentCard)
self.currentCard.tier += 1
self.currentCard.deckNo = self.currentSession
leitner.progressDeck[self.currentSession].push(self.currentCard)
leitner.commit_card_update(self.currentCard)
self.create_feedback_popup('Correct!')
elif self.currentCard.tier == 3:#mastered
leitner.progressDeck[self.currentCard.deckNo].remove(self.currentCard)
self.currentCard.tier += 1
self.currentCard.deckNo = -2 #retired
leitner.deckRetired.push(self.currentCard)
leitner.commit_card_update(self.currentCard)
self.create_feedback_popup('Correct!\nYou have mastered this word:' + self.currentCard.word)
else:
leitner.progressDeck[self.currentCard.deckNo].remove(self.currentCard)
self.currentCard.tier += 1
leitner.progressDeck[self.currentCard.deckNo].push(self.currentCard)
leitner.commit_card_update(self.currentCard)
self.create_feedback_popup('Correct!')
else:
if self.currentCard.deckNo >= 0:
leitner.progressDeck[ self.currentCard.deckNo ].remove(self.currentCard)
self.currentCard.tier = 0
self.currentCard.deck = -2#current
leitner.deckCurrent.push(self.currentCard)
leitner.commit_card_update(self.currentCard)
self.create_feedback_popup('Unfortunately this is incorrect.\nThe answer is:\n'+self.currentCard.translation)
#meg kell csinalni
def create_feedback_popup(self, _text):
# print('feedback popup would have been created: ' + _text)
self.feedbackPopup = tk.Toplevel(self, width = 150, height = 120, takefocus = True)
feedbackLabel = tk.Label(self.feedbackPopup, text = _text)
feedbackLabel.place(width = 130, height = 60, relx = 0.5, y = 40, anchor = CENTER)
closeFeedbackButton = tk.Button(self.feedbackPopup, text = 'OK')
closeFeedbackButton.bind('<KeyRelease-Return>', self.close_feedback_popup)
closeFeedbackButton.bind('<ButtonRelease-1>', self.close_feedback_popup)
closeFeedbackButton.place(relx = 0.5, rely = 1, y = -20, anchor = CENTER)
closeFeedbackButton.focus()
def close_feedback_popup(self, event):
self.feedbackPopup.destroy()
self.display_next_query()
def quit_session(self, event):
self.queryPopup.destroy()
def create_insertion_popup(self, event):
self.insertionPopup = tk.Toplevel(self, width = 300, height = 200, takefocus = True)
wordLabel = tk.Label(self.insertionPopup, text = 'Word: ')
wordLabel.place(width = 100, height = 20, relx = 0, rely = 0.33, x=5, anchor = NW)
self.wordInput = tk.Entry(self.insertionPopup)
self.wordInput.place(width = 150, height = 20, relx = 0, rely = 0.33, x=120, anchor = NW)
self.wordInput.focus()
translationLabel = tk.Label(self.insertionPopup, text = 'Translation: ')
translationLabel.place(width = 100, height = 20, relx = 0, rely = 0.66, x=5, anchor = NW)
self.translationInput = tk.Entry(self.insertionPopup)
self.translationInput.place(width = 150, height = 20, relx = 0, rely = 0.66, x = 120, anchor = NW)
self.translationInput.bind('<KeyRelease-Return>', self.insert_flashcard)
insertButton = tk.Button(self.insertionPopup, text = 'Add')
insertButton.bind('<ButtonRelease-1>', self.insert_flashcard)
insertButton.bind('<KeyRelease-Return>', self.insert_flashcard)
insertButton.place(relx = 0.5, rely = 1, y = -30, anchor = CENTER)
def insert_flashcard(self, event):
_word = self.wordInput.get()
_translation = self.translationInput.get()
_card = flashcard.Flashcard(0, _word, _translation)
db.insert_new_card(_card)
self.insertionPopup.destroy()
leitner.initialize()
print('Database reloaded.')
root = tk.Tk()
app = Application(master=root)