-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVocabularyBuilder.py
502 lines (388 loc) · 18.9 KB
/
VocabularyBuilder.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#!/usr/bin/python
# Python3
import codecs
import os
import sys
import time
from random import sample, randint
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gio, Gdk
class MainWindow(Gtk.ApplicationWindow):
def __init__(self):
self.draw_main_window_headerbar() # draw window
self.define_variable() # set variables
self.read_file_and_import_data() # read data from file
self.generate_training_data()
#self.ask_what_you_want() # on startup ask what you want?
self.set_vbox()
self.start_learning(Gtk.Button()) #instead of above let's start with quiz
def set_vbox(self):
#print("vbox")
self.vbox1 = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing = 0)
self.add(self.vbox1)
def start_learning(self, button):
self.count = 0
self.change_to_learning_headerbar()
self.generate_values(self.data)
self.remove(self.vbox1)
self.set_vbox()
self.hb.props.title = "Vocabulary Builder 19"
self.hb.props.subtitle = "Learning"
# self.learningLabel = Gtk.Label()
# self.learningLabel.set_label("Learning")
# self.vbox1.pack_start(self.learningLabel, False, False, 0)
self.show_learning_labels()
#self.show_quiz_labels() #start with quiz
####
self.hbox0 = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
self.hbox0.set_property("width-request", 500)
self.hbox0.set_property("height-request", 10)
self.vbox1.pack_start(self.hbox0, False, False, 0)
self.hbox0.pack_start(self.word_label, True, True, 0)
self.hbox0.pack_start(self.word_difficulty, True, True, 0)
self.vbox1.pack_start(self.word_meaning, False, False, 0)
self.show_all()
print(self.word)
def start_quiz(self,button):
self.count=0
self.hb.props.title = "Vocabulary Builder 30"
self.hb.props.subtitle = "Quiz"
self.change_to_quiz_headerbar()
self.generate_values(self.data)
self.remove(self.vbox1)
self.vbox1 = Gtk.Fixed()
self.add(self.vbox1)
self.button=['0','1','2','3','4']
# add another horizontal box on 1st vbox
self.vbox0 = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
self.hbox0 = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=150)
for x in range(0,5):
self.button[x]=Gtk.CheckButton.new()
self.vbox1.put(self.hbox0,0,0)
self.vbox1.put(self.vbox0,0,50)
self.hbox0.pack_start(self.word_label, True, True, 0)
self.hbox0.pack_start(self.word_difficulty, True, True, 0)
self.show_quiz_labels()
#self.show_all()
def on_button_toggled(self, button, name):
if button.get_active():
#debug# print("Im in toggled", self.correct)
if self.correct != name:
print("Incorrect!")
#self.word_difficulty.set_markup("<span size=\"xx-small\"><b>" + self.difficulty + "</b></span>")
output=button.get_label()
#print(str(output))
button.get_child().set_markup("<span><s>"+output +"</s></span>")
elif self.correct==name:
print("Correct!!!!")
#button.set_active(True)
for x in range(0,5):
self.button[x].set_state_flags(Gtk.StateFlags.INSENSITIVE, True)
#self.button[x].set_sensitive(False)
# self.button[self.correct].set_sensitive(False)
self.button[self.correct].set_state_flags(Gtk.StateFlags.CHECKED, True)
#button.set_state_flags(Gtk.StateFlags.CHECKED, False)
#self.show_quiz_labels()
def read_file_and_import_data(self):
#Open file and load Data#
#-- File opening process --#
try:
fileName = "./vocab_data.tsv"
file = codecs.open(fileName, "rb", encoding='UTF-8')
except IndexError:
print("Error: Please provide filename as argument")
sys.exit(2)
except IOError:
print("Error: Cannot open file:", fileName)
sys.exit(2)
# load content from file
for line in file:
line=line.strip(os.linesep) #remove \r\n \n
line = line.strip() #remove trailing spaces and tabs
try:
# expecting 5 tabs in a line
# 1st as word, 2nd as part of speech and 3rd as meaning
#print(20* "LINE ")
#print(line)
(one,two,three,four,five,six) = line.split('\t')
x = [one,two,three,four,five,six]
#print(x)
self.fileData.append(x)
except ValueError:
#If line does not contain 6 values separated by 5 tabs then show that line
print("*** Each Line with 6 values (word, pos, mean, example, difficutly, mastered) separated by Tabs")
print("the bad line is")
print('*'*50)
print(line)
print('*'*50)
file.close() #better to close file before exiting
sys.exit(2)
file.close()
#Close file
def define_variable(self):
# Variables used in this program
self.fileData=[] # Stores the date from file
self.data=[]
self.css="" #New list (data) from file data
self.second=""
self.third=""
self.fourth=""
self.fifth=""
self.word =""
self.difficulty=""
self.meaning=""
self.what = ''
self.answer=""
self.word_label = Gtk.Label()
self.word_difficulty = Gtk.Label()
self.word_meaning = Gtk.Label()
self.icon = ""
self.count=0
def draw_main_window_headerbar(self):
Gtk.Window.__init__(self, title="RadioButton Demo")
self.hb = Gtk.HeaderBar()
self.hb.set_show_close_button(True)
self.hb.props.title = "Vocabulary Builder 0.18"
self.set_titlebar(self.hb)
self.set_border_width(15)
self.set_size_request(500,400)
self.set_default_size(500,400)
self.set_resizable(True)
self.set_position(Gtk.WindowPosition.CENTER)
# Back and next Buttons
self.buttonBack = Gtk.Button.new()
self.backIcon = Gio.ThemedIcon(name="go-previous-symbolic")
self.backImage= Gtk.Image.new_from_gicon(self.backIcon, Gtk.IconSize.BUTTON)
self.buttonBack.add(self.backImage)
self.hb.pack_start(self.buttonBack)
#self.buttonNext = Gtk.Button.new_with_label("Next >")
self.buttonNext = Gtk.Button.new()
self.nextIcon = Gio.ThemedIcon(name="go-next-symbolic")
self.nextImage= Gtk.Image.new_from_gicon(self.nextIcon, Gtk.IconSize.BUTTON)
self.buttonNext.add(self.nextImage)
self.hb.pack_start(self.buttonNext)
# Menu button on headerbar
self.menu_button = Gtk.MenuButton()
self.icon = Gio.ThemedIcon(name="open-menu-symbolic")
self.image = Gtk.Image.new_from_gicon(self.icon, Gtk.IconSize.BUTTON)
self.menu_button.add(self.image)
self.hb.pack_end(self.menu_button)
# connecting popover menu to menu_button
#self.menu_button.connect("toggled", self.on_click_menubutton_on_headerbar)
# Hide Close button
#self.hb.set_show_close_button(False)
# set button style
self.menu_button.set_relief(Gtk.ReliefStyle.NORMAL)
#popover
self.popover = Gtk.Popover()
#in the pop up menu add menu items with button
vbox = Gtk.Box(spacing=1, orientation=Gtk.Orientation.VERTICAL)
vbox.set_border_width(5)
menu_item_button1=Gtk.ModelButton()
menu_item_button1.set_label("Learn Vocabulary")
menu_item_button1.connect("clicked", self.start_learning)
vbox.pack_start(menu_item_button1, True, True, 5)
menu_item_button2=Gtk.ModelButton()
menu_item_button2.set_label("Take Quiz")
menu_item_button2.connect("clicked", self.start_quiz)
vbox.pack_start(menu_item_button2, True, True, 5)
menu_item_button3=Gtk.CheckButton()
menu_item_button3.set_label("Dark Theme")
menu_item_button3.connect("toggled", self.set_dark_theme)
vbox.pack_start(menu_item_button3, True, True, 5)
#add quit menu item
menu_item_button4=Gtk.ModelButton()
menu_item_button4.set_label("Quit")
menu_item_button4.connect("clicked", Gtk.main_quit)
vbox.pack_start(menu_item_button4, True, True, 5)
#put the buttons in popover menu using vbox container
self.popover.add(vbox)
self.popover.set_position(Gtk.PositionType.BOTTOM)
# add popover to menu button
self.menu_button.set_popover(self.popover)
# show the widgets
self.popover.show_all()
def change_to_learning_headerbar(self):
self.hb.remove(self.buttonNext)
self.hb.remove(self.buttonBack)
self.count=0
self.buttonBack = Gtk.Button.new()
self.backIcon = Gio.ThemedIcon(name="go-previous-symbolic")
self.backImage= Gtk.Image.new_from_gicon(self.backIcon, Gtk.IconSize.BUTTON)
self.buttonBack.add(self.backImage)
self.hb.pack_start(self.buttonBack)
#self.buttonNext = Gtk.Button.new_with_label("Next >")
self.buttonNext = Gtk.Button.new()
self.nextIcon = Gio.ThemedIcon(name="go-next-symbolic")
self.nextImage= Gtk.Image.new_from_gicon(self.nextIcon, Gtk.IconSize.BUTTON)
self.buttonNext.add(self.nextImage)
self.hb.pack_start(self.buttonNext)
#self.buttonNext.set_label("LNext")
self.buttonBack.connect("clicked", self.on_click_me_clicked_back_learn)
self.buttonNext.connect("clicked", self.on_click_me_clicked_next_learn)
def on_click_me_clicked_back_learn(self,widget):
# generate values only when count>0
if self.count >=1:
self.count = self.count-1
self.generate_values(self.data)
self.show_learning_labels()
def on_click_me_clicked_back_quiz(self,widget):
# generate values only when count>0
if self.count >=1:
self.count = self.count-1
self.generate_values(self.data)
self.show_quiz_labels()
def change_to_quiz_headerbar(self):
self.hb.remove(self.buttonNext)
self.hb.remove(self.buttonBack)
#self.buttonNext.set_label("Qnext")
self.count=0
self.buttonBack = Gtk.Button.new()
self.backIcon = Gio.ThemedIcon(name="go-previous-symbolic")
self.backImage= Gtk.Image.new_from_gicon(self.backIcon, Gtk.IconSize.BUTTON)
self.buttonBack.add(self.backImage)
self.hb.pack_start(self.buttonBack)
#self.buttonNext = Gtk.Button.new_with_label("Next >")
self.buttonNext = Gtk.Button.new()
self.nextIcon = Gio.ThemedIcon(name="go-next-symbolic")
self.nextImage= Gtk.Image.new_from_gicon(self.nextIcon, Gtk.IconSize.BUTTON)
self.buttonNext.add(self.nextImage)
self.hb.pack_start(self.buttonNext)
self.buttonBack.connect("clicked", self.on_click_me_clicked_back_quiz)
self.buttonNext.connect("clicked", self.on_click_me_clicked_next_quiz)
def on_click_me_clicked_next_learn(self,widget):
# generate values only when count>0
if self.count < self.indexMax:
self.count = self.count+1
self.generate_values(self.data)
self.show_learning_labels()
def on_click_me_clicked_next_quiz(self,widget):
# generate values only when count>0
if self.count < self.indexMax:
self.count = self.count+1
self.generate_values(self.data)
self.show_quiz_labels()
def set_dark_theme(self,button):
if button.get_active():
settings = Gtk.Settings.get_default()
settings.set_property("gtk-application-prefer-dark-theme", True)
else:
settings = Gtk.Settings.get_default()
settings.set_property("gtk-application-prefer-dark-theme", False)
def show_learning_labels(self):
#self.word_label.set_markup("<span foreground=\"green\" font-desc=\"Arimo Bold 20\" size=\"x-large\">"+ self.word + "</span>")
self.word_label.set_markup("<span size=\"xx-large\"><b>" + self.word + "</b></span>")
self.word_label.set_halign(Gtk.Align.START)
self.word_label.set_justify(Gtk.Justification.LEFT)
#self.word_label.set_property("width-request", 0)
#self.word_label.set_property("height-request", 10)
#self.word_difficulty.set_markup("<span font-desc=\"Arimo 10\" size=\"x-large\"> ""[" + self.difficulty + "]""</span>")
self.word_difficulty.set_markup("<span size=\"xx-small\"><b>" + self.difficulty + "</b></span>")
self.word_difficulty.set_halign(Gtk.Align.END)
self.word_difficulty.set_justify(Gtk.Justification.RIGHT)
self.word_difficulty.set_line_wrap(True)
# multiple meanings from single line to multiple lines
#self.answer=self.answer.replace('; ', '\n')
#self.word_meaning.set_markup("<span font-desc=\"Arimo 14\" size=\"x-large\">" + self.answer + "</span>")
#self.word_difficulty.set_markup("[" + self.difficulty + "]")
self.word_meaning.set_markup(self.answer)
self.word_meaning.set_halign(Gtk.Align.START)
self.word_meaning.set_justify(Gtk.Justification.FILL)
self.word_meaning.set_line_wrap(True)
def show_quiz_labels(self):
for x in range(0,5):
#self.button[x].set_state_flags(Gtk.StateFlags.INSENSITIVE, False)
self.button[x].set_active(False)
self.word_label.set_markup("<span size=\"xx-large\"><b>" + self.word + "</b></span>")
#self.word_label.set_markup("<span foreground=\"green\" font-desc=\"Arimo Bold 20\" size=\"x-large\"> "+ self.word + "</span>")
self.word_label.set_selectable(True)
self.word_label.set_halign(Gtk.Align.START)
self.word_label.set_justify(Gtk.Justification.LEFT)
#self.word_label.set_property("width-request", 0)
#self.word_label.set_property("height-request", 10)
self.word_difficulty.set_markup("<span size=\"xx-small\"><b>" + self.difficulty + "</b></span>")
#self.word_difficulty.set_markup("<span font-desc=\"Arimo Bold 10\" size=\"x-large\"> ""[" + self.difficulty + "]""</span>")
#self.word_difficulty.set_markup("[" + self.difficulty + "]")
self.word_difficulty.set_halign(Gtk.Align.END)
self.word_difficulty.set_justify(Gtk.Justification.RIGHT)
self.word_difficulty.set_line_wrap(True)
#self.word_difficulty.set_property("width-request", 500)
#self.word_difficulty.set_property("height-request", 200)
for x in range(0,5): #rizvan
self.vbox0.remove(self.button[x])
self.button[x]=Gtk.CheckButton.new()
self.button[x].connect("clicked", self.on_button_toggled, x)
self.button[x].set_label(self.randXList[x])
self.button[x].get_child().set_justify(Gtk.Justification.FILL)
self.button[x].get_child().set_line_wrap(True)
#self.button[x].get_child().set_single_line_mode(True)
self.button[x].set_property("width-request", 500)
self.button[x].set_property("height-request", 00)
self.vbox0.pack_start(self.button[x], False, False, 0)
self.show_all()
self.hbox0.set_property("width-request", 500)
self.hbox0.set_property("height-request", 10)
#headerbar button "click me"
#self.buttonOK = Gtk.Button.new_with_label("Next >")
#self.buttonOK.connect("clicked", self.on_click_me_clicked)
# Afer generating new data remove radio selection
for x in range(0,5):
self.button[x].set_state_flags(Gtk.StateFlags.SELECTED, True)
def generate_training_data(self):
#New list (data) from file data
needItems=10
idxMax = len(self.fileData)-1 # total number of items in data list, -1 (0-25=26)
r=sample(range(0, idxMax), needItems)
for x in range(0,needItems):
self.data.append(self.fileData[r[x]])
#print(x)
self.indexMax=len(self.data)-1
def generate_values(self,data):
#print("Hello values")
#Max number of entries in the list
# next and back needs count
if self.count > self.indexMax: #if reached end of list
self.count = self.indexMax
elif self.count < 0: #if reached the beginning of list
self.count = 0
# Disable Buttons based on counter
# Back button
if self.count == 0:
self.buttonBack.set_sensitive(False)
elif self.count > 0:
self.buttonBack.set_sensitive(True)
# Next button
if self.count == self.indexMax:
self.buttonNext.set_sensitive(False)
elif self.count < self.indexMax:
self.buttonNext.set_sensitive(True)
#Get 5 random unique index values
randList=sample(range(0, self.indexMax), 5)
#print(self.data)
# Get quiz word and answer and wrong answers
quizWord=self.data[self.count][0] # quiz word
self.answer=self.data[self.count][1] # correct answer
self.difficulty=self.data[self.count][4] # category of word to be displayed on right side
answer1=self.fileData[randList[1]][1] # wrong answer 1
answer2=self.fileData[randList[2]][1] # wrong answer 2
answer3=self.fileData[randList[3]][1] # wrong answer 3
answer4=self.fileData[randList[4]][1] # wrong answer 4
#randomize radiobuttons
randList2=sample(range(0, 5), 5)
xlist=[self.answer, answer1, answer2, answer3, answer4]
self.randXList=[xlist[randList2[0]], xlist[randList2[1]],xlist[randList2[2]],xlist[randList2[3]],xlist[randList2[4]]]
#print(answer0)
#print(xlist)
self.first=self.randXList[0]
self.second=self.randXList[1]
self.third=self.randXList[2]
self.fourth=self.randXList[3]
self.fifth=self.randXList[4]
self.correct=self.randXList.index(self.answer) #we have 1 2 3 4 here it is 0 1 2 3 hence add 1
self.word=quizWord
# Main Program
win = MainWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()