-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
165 lines (144 loc) · 6.57 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
import tkinter as tk
from random import choice
import pandas as pd
import pinyin
from google_speech import Speech
import sox
#import re
BACKGROUND_COLOR = "#B1DDC6"
# Read csv and convert to dictionary
try:
df = pd.read_csv('data/words_to_learn.csv')
except FileNotFoundError:
df = pd.read_csv('data/chinese_words.csv')
to_learn = df.to_dict(orient="records")
# # Delete Chinese words that have english characters
# chinese_words = df['Chinese'].tolist()
# frequency_count = df['Frequency Count'].tolist()
# english_words = df['English'].tolist()
# for index, item in enumerate(chinese_words):
# a = re.search('[a-zA-Z]', item)
# if a != None:
# del chinese_words[index]
# del frequency_count[index]
# del english_words[index]
# # Create new DataFrame and export to csv
# new_df = pd.DataFrame({'Chinese': chinese_words, 'English': english_words, 'Frequency Count': frequency_count})
# new_df.to_csv("data/chinese_words.csv", index=False)
# Keep track of progress variables
correct_words = []
incorrect_words = []
side = 'front'
current_card = None
current_pinyin = None
show_pinyin = False
class App(object):
def __init__(self):
window.title("Flashy")
window.config(padx=50, pady=50, bg=BACKGROUND_COLOR)
self.canvas = tk.Canvas(width=800, height=526, bg=BACKGROUND_COLOR, highlightthickness=0)
# image_img = PhotoImage(file="images/images.png")
self.card_back_img = tk.PhotoImage(file="images/card_back.png")
self.card_front_img = tk.PhotoImage(file="images/card_front.png")
self.card_image = self.canvas.create_image(400, 263, image=self.card_front_img)
self.card_title = self.canvas.create_text(400, 150, fill="#000000", font=("Ariel", 40, "italic"))
self.card_word = self.canvas.create_text(400, 263, fill="#000000", font=("Ariel", 60, "bold"))
self.card_pinyin_label = self.canvas.create_text(400, 363, fill="#000000", font=("Ariel", 60, "bold"))
self.canvas.itemconfig(self.card_pinyin_label, state='hidden')
self.canvas.bind("<Button-1>", self.on_click)
# Buttons
self.correct_img = tk.PhotoImage(file="images/right.png")
self.incorrect_img = tk.PhotoImage(file="images/wrong.png")
self.volume_img = tk.PhotoImage(file="images/volume_icon.png")
self.volume_img = self.volume_img.subsample(10)
self.wrong_button = tk.Button(highlightbackground=BACKGROUND_COLOR, bg=BACKGROUND_COLOR, image=self.incorrect_img, command=self.incorrect)
self.correct_button = tk.Button(highlightbackground=BACKGROUND_COLOR, bg=BACKGROUND_COLOR, image=self.correct_img, command=self.correct)
self.show_pinyin_button = tk.Button(window, highlightbackground="#FFFFFF", bg="#FFFFFF", text="Show Pinyin", command=self.show_pinyin) # , height=3, width=10)
self.narrator_button = tk.Button(window, highlightbackground="#FFFFFF", bg="#FFFFFF", image=self.volume_img, command=self.narrate)
# Grid Layout
self.canvas.grid(column=0, row=0, columnspan=15, rowspan=10)
self.wrong_button.grid(column=5, row=10)
self.correct_button.grid(column=9, row=10)
self.show_pinyin_button.grid(column=7, row=7)
self.narrator_button.grid(column=7, row=8)
# Set up flashcards
self.next_card()
def show_pinyin(self):
global show_pinyin
show_pinyin = True
self.show_pinyin_button.lower() # Lowers button below canvas. i.e. out of view. Much easier than destroying
# self.show_pinyin_button.destroy()
self.canvas.itemconfig(self.card_pinyin_label, state='normal')
def narrate(self):
global side
global current_card
lang = ''
text = ''
if side == 'front':
lang = "zh"
text = current_card["Chinese"]
elif side == 'back':
lang = "en"
text = current_card["English"]
speech = Speech(text, lang)
speech.play()
def flip_card(self):
global side
global current_card
global current_pinyin
global show_pinyin
if side == 'front':
side = 'back'
self.canvas.itemconfig(self.card_image, image=self.card_back_img)
self.canvas.itemconfig(self.card_title, text="English", fill="#FFFFFF")
self.canvas.itemconfig(self.card_word, text=current_card["English"], fill="#FFFFFF")
self.canvas.itemconfig(self.card_pinyin_label, text="", fill="#000000")
if not show_pinyin:
# If pinyin is not shown, hide button on back
self.show_pinyin_button.lower()
else:
side = 'front'
self.canvas.itemconfig(self.card_image, image=self.card_front_img)
self.canvas.itemconfig(self.card_title, text="Chinese", fill="#000000")
self.canvas.itemconfig(self.card_word, text=current_card["Chinese"], fill="#000000")
self.canvas.itemconfig(self.card_pinyin_label, text=current_pinyin, fill="#000000")
if not show_pinyin:
# If pinyin is shown, show again on front
self.show_pinyin_button.lift()
def next_card(self):
global current_card
global side
global current_pinyin
global show_pinyin
if side == 'back':
self.flip_card()
current_card = choice(to_learn)
self.canvas.itemconfig(self.card_title, text="Chinese")
self.canvas.itemconfig(self.card_word, text=current_card["Chinese"])
current_pinyin = pinyin.get(current_card["Chinese"], delimiter=" ")
self.canvas.itemconfig(self.card_pinyin_label, text=current_pinyin, fill="#000000")
# Reset Pinyin button states for next card
self.canvas.itemconfig(self.card_pinyin_label, state='hidden')
self.show_pinyin_button.lift()
show_pinyin = False
def correct(self):
global current_card
correct_words.append(current_card)
to_learn.remove(current_card)
correct_df = pd.DataFrame(correct_words)
correct_df.to_csv("data/correct_words.csv", index=False)
self.next_card()
def incorrect(self):
global current_card
incorrect_words.append(current_card)
to_learn.remove(current_card)
incorrect_df = pd.DataFrame(incorrect_words)
incorrect_df.to_csv("data/incorrect_words.csv", index=False)
words_to_learn_df = pd.DataFrame(to_learn)
words_to_learn_df.to_csv("data/words_to_learn.csv", index=False)
self.next_card()
def on_click(self, event):
self.flip_card()
window = tk.Tk()
a = App()
window.mainloop()