-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVocabularyController.py
104 lines (90 loc) · 3.46 KB
/
VocabularyController.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
import VocabularyModel as Model
import VocabularyView as View
import random
import tkinter as tk
class VocabularyController:
"""Implement the app Controller"""
def __init__(self):
self.english = True
self.id = ''
self.root = tk.Tk()
self.model = Model.VocabularyModel()
self.view = View.VocabularyView(self.root)
self.view.checkButton.config(command=self.checkWords)
self.view.addButton.config(command=self.addWord)
self.word = ''
self.translation = ''
self.showNewWord()
self.view.changeButton.config(command = self.changeLanguage)
self.view.restartButton.config(command = self.restartSystem)
def restartSystem(self):
self.model.restartIds()
self.showNewWord()
self.view.setResultString("Restarted system")
def showNewWord(self):
if self.english:
self.view.setWordString(self.getWord(self.english))
self.view.setTranslationString("")
else:
self.view.setTranslationString(self.getWord(self.english))
self.view.setWordString("")
def showWord(self):
if self.english:
self.view.setWordString(self.word)
self.view.setTranslationString("")
else:
self.view.setTranslationString(self.translation)
self.view.setWordString("")
def changeLanguage(self):
self.english = not self.english
self.model.restartIds()
self.showNewWord()
self.view.setResultString("Language switched")
def checkWords(self):
word = self.view.getWordString()
translation = self.view.getTranslationString()
if self.word == word and self.translation == translation:
if not self.model._ids and not self.model._idsFails:
self.view.setResultString("Well done! Click Restart")
else:
self.view.setResultString("Correct")
self.showNewWord()
else:
self.view.setResultString("Incorrect. %s is %s" % (self.word, self.translation))
self.model.setAddIdsFails(self.id)
self.showNewWord()
def getWord(self, ingles=True):
word = ""
if not self.model.ids:
if self.model.idsFails:
self.model.updateIds()
if self.model.ids:
self.id = self.calculateId()
words = self.model.readWord(self.id)
self.word = words['word']
self.translation = words['translation']
if ingles:
word = self.word
else:
word = self.translation
return word
def addWord(self):
word = self.view.getWordString()
translation = self.view.getTranslationString()
if translation != "" and word != "":
self.model.createWord(word,translation)
self.view.setResultString("Word added %s - %s" % (word,translation))
self.showNewWord()
else:
self.view.setResultString("The fields can\'t be empty")
self.showWord()
def calculateId(self):
ids = self.model.ids
return random.choice(ids)
def run(self):
self.root.title("Vocabulary")
self.root.deiconify()
self.root.mainloop()
if __name__ == "__main__":
c = VocabularyController()
c.run()