1 # Copyright (C) 2015 mUniKeS
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 from random import shuffle
18 from unicodedata import lookup
19 from Tkinter import Tk, Button, PhotoImage, Label, Frame
22 TITLE = "Learn Tibetan"
23 WINDOW_DIMENSIONS = "185x260+100+80"
24 SOUNDS_PATH = os.path.join(os.path.dirname(__file__), "../sounds/")
25 IMAGES_PATH = os.path.join(os.path.dirname(__file__), "../images/")
27 LETTERS = {lookup("tibetan letter ka"):("ka", SOUNDS_PATH + "ka.ogg"),
28 lookup("tibetan letter kha"):("k'a", SOUNDS_PATH + "kha.ogg"),
29 lookup("tibetan letter ga"):("k_'a", SOUNDS_PATH + "kh_a.ogg"),
30 lookup("tibetan letter nga"):("nga", SOUNDS_PATH + "nga.ogg")}
35 def shuffle_dictionary (dictionary):
37 Return random keys of dictionary
40 # keys = list(dictionary.keys())
42 keys = dictionary.keys()
47 def generate_letters(list_letters, NUM_LETTERS):
49 Return a right letter and a list of shuffle letters
51 right_letter = list_letters[0]
52 letters = list_letters[0:NUM_LETTERS]
54 return right_letter, letters
57 def play_ogg(list_oggs):
62 SONG_END = pygame.USEREVENT + 1
63 pygame.mixer.music.set_endevent(SONG_END)
64 pygame.mixer.music.load(list_oggs[i])
65 pygame.mixer.music.play()
69 for event in pygame.event.get():
70 if i >= len(list_oggs) - 1:
72 elif event.type == SONG_END:
73 pygame.mixer.music.load(list_oggs[i+1])
74 pygame.mixer.music.play()
78 class Application(Frame):
80 def __init__(self, parent):
82 Frame.__init__(self, parent)
85 # generate the options of buttons
86 keys = shuffle_dictionary(LETTERS)
87 right_answer, answers = generate_letters(keys, NUM_LETTERS)
90 self.create_letter(right_answer)
92 self.create_play_button(answers)
93 self.create_option_buttons(right_answer, answers)
95 def create_window (self):
97 self.parent.geometry(WINDOW_DIMENSIONS)
98 self.parent.title(TITLE)
100 def create_letter(self, right_answer):
102 self.label = Label(self.parent, text=right_answer, font=("",80))
105 def create_button(self):
107 self.button = Button().place(x=5,y=102)
109 def create_play_button(self, answers):
111 # create list of oggs
114 for i in range(0, NUM_LETTERS):
115 list_oggs.append(LETTERS[answers[i]][1])
117 self.play_image = PhotoImage(file=IMAGES_PATH + 'play.png')
119 self.play_button = Button(self.parent,width=40,height=30,fg='black',
120 image=self.play_image, command=lambda: play_ogg(list_oggs)).place(x=5,y=102)
122 def right_button(self, button):
124 Button right green and sound
126 button.config(background = "green")
127 button.config(activebackground="green")
128 pygame.mixer.music.load(SOUNDS_PATH + 'right.ogg')
129 pygame.mixer.music.play()
131 def wrong_button(self, button):
133 Button wrong red and sound
135 button.config(background = "red")
136 button.config(activebackground="red")
137 pygame.mixer.music.load(SOUNDS_PATH + 'wrong.ogg')
138 pygame.mixer.music.play()
140 def create_option_buttons(self, right_answer, answers):
143 self.option_1 = Button(self.parent,width=7,height=3,fg='black',
144 text=LETTERS[answers[0]][0])
145 self.option_1.place(x=5,y=140)
146 self.option_2 = Button(self.parent,width=7,height=3,fg='black',
147 text=LETTERS[answers[1]][0])
148 self.option_2.place(x=5,y=200)
149 self.option_3 = Button(self.parent,width=7,height=3,fg='black',
150 text=LETTERS[answers[2]][0])
151 self.option_3.place(x=95,y=140)
152 self.option_4 = Button(self.parent,width=7,height=3,fg='black',
153 text=LETTERS[answers[3]][0])
154 self.option_4.place(x=95,y=200)
155 self.option_1.config(command=lambda:
156 self.right_button(self.option_1) if answers[0] == right_answer
157 else self.wrong_button(self.option_1))
158 self.option_2.config(command=lambda:
159 self.parent.right_button(self.option_2) if answers[1] == right_answer
160 else self.wrong_button(self.option_2))
161 self.option_3.config(command=lambda:
162 self.right_button(self.option_3) if answers[2] == right_answer
163 else self.wrong_button(self.option_3))
164 self.option_4.config(command=lambda:
165 self.right_button(self.option_4) if answers[3] == right_answer
166 else self.wrong_button(self.option_4))
173 app = Application(root)
178 if __name__ == '__main__':