-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
219 lines (176 loc) · 6.86 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
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
import tkinter as tk
from random import randint
import pandas as pd
from text_to_png import to_png
BACKGROUND_COLOR = "#B1DDC6"
FONT_TITLE = ("Arial", 40, "bold")
FONT_WORD = ("Arial", 60)
FONT_PARTSOFSPEECH = ("Arial", 20)
def get_ar_word():
"""Reads the learned_words.txt to make learned words list of str with ids. If the current word is found in the learned words it will choose a different word.
If the word is not in the learned words list and the length of the learned words are lower than total records then display new arabic word and start the timer.
"""
global curr_record
global curr_ar_image
global flip_timer
try:
with open("data/learned_words.txt", mode="r") as learned_file:
learned_words = [
line.replace("\n", "") for line in learned_file.readlines()
]
except FileNotFoundError:
with open("data/learned_words.txt", mode="w") as learned_file:
learned_words = []
pass
# print(learned_words)
curr_record = randint(0, total_records)
found_curr_word = True
while found_curr_word:
found_curr_word = False
for word in learned_words:
# print(f"get ar {type(word)} = {type(id_data[curr_record])}")
# print(f"get ar {(word)} = {(id_data[curr_record])}")
if word == str(id_data[curr_record]):
curr_record = randint(0, total_records)
found_curr_word = True
# print(f"{found_curr_word=}")
# print()
# print(total_records)
if total_records < len(learned_words):
curr_ar_image = to_png(text=" ", color=(0, 0, 0))
canvas.itemconfig(
language_canvas, text="You Learned All Words!", fill="black"
)
canvas.itemconfig(pos_canvas, text="", fill="black")
reset = tk.Button(
text="Reset",
command=reset_learned,
relief="flat",
background=BACKGROUND_COLOR,
activebackground=BACKGROUND_COLOR,
highlightthickness=0,
)
reset.grid(column=1, row=2)
window.after_cancel(flip_timer)
return
window.after_cancel(flip_timer)
canvas.itemconfig(language_canvas, text=arabic_words.name, fill="black")
curr_ar_image = to_png(text=arabic_words[curr_record], color=(0, 0, 0))
canvas.itemconfig(word_canvas, image=curr_ar_image)
canvas.itemconfig(pos_canvas, text=word_is_a[curr_record], fill="black")
canvas.itemconfig(card_canvas, image=card_q)
flip_timer = window.after(6000, flip_card)
def not_knew_it():
"""Cancels the timer. Appends the current word to need_to_learn.txt. If the file does not exists, creates it. Calls get_ar_word()."""
global flip_timer
window.after_cancel(flip_timer)
try:
with open("data/need_to_learn.txt", mode="a") as data_file:
data_file.write(
f"{arabic_words[curr_record]},{english_words[curr_record]}\n"
)
except FileNotFoundError:
with open("data/need_to_learn.txt", mode="w") as data_file:
data_file.writelines(
[
"Arabic,English",
f"{arabic_words[curr_record]},{english_words[curr_record]}\n",
]
)
get_ar_word()
def flip_card():
"""Gets the english meaning of the current arabic word and displays it."""
global curr_ar_image
canvas.itemconfig(language_canvas, text=english_words.name, fill="white")
curr_ar_image = to_png(text=english_words[curr_record], color=(255, 255, 255))
canvas.itemconfig(word_canvas, image=curr_ar_image)
canvas.itemconfig(pos_canvas, text=word_is_a[curr_record], fill="white")
canvas.itemconfig(card_canvas, image=card_a)
def knew_it():
"""Searches for the current word in the learned word. If its there then breaks and calls get_ar_word(). Else appends the new word to learned_words.txt."""
try:
with open("data/learned_words.txt", mode="r") as data_file:
found_learned_word = False
for line in data_file.readlines():
# print(f"knew it {line} = {id_data[curr_record]}")
if str(id_data[curr_record]) == line.replace("\n", ""):
found_learned_word = True
break
if found_learned_word is False:
# print("writing")
with open("data/learned_words.txt", mode="a") as data_file:
data_file.write(f"{id_data[curr_record]}\n")
except FileNotFoundError:
with open("data/learned_words.txt", mode="w") as data_file:
data_file.write(
f"{arabic_words[curr_record]}\n",
)
get_ar_word()
def reset_learned():
"""Clears all the data in learned_words.txt and need_to_learn.txt. Starts the timer and calls get_ar_word()"""
global flip_timer
with open("data/learned_words.txt", mode="w"):
pass
with open("data/need_to_learn.txt", mode="w"):
pass
flip_timer = window.after(3000, flip_card)
get_ar_word()
with open("data/need_to_learn.txt", mode="w"):
pass
# TODO UI setup
window = tk.Tk()
window.title("Learn Words From The Quran")
window.config(padx=40, pady=40, background=BACKGROUND_COLOR)
card_q = tk.PhotoImage(file="images/card_front.png")
card_a = tk.PhotoImage(file="images/card_back.png")
canvas = tk.Canvas(
width=800, height=526, background=BACKGROUND_COLOR, highlightthickness=0
)
card_canvas = canvas.create_image(400, 263, image=card_q)
language_canvas = canvas.create_text(400, 150, text="Language", font=FONT_TITLE)
word_canvas = canvas.create_image(400, 263) # Placeholder
pos_canvas = canvas.create_text(
400, 313, text="(Ready To Learn!)", font=FONT_PARTSOFSPEECH
)
canvas.grid(column=0, row=0, columnspan=3)
correct_img = tk.PhotoImage(file="images/right.png")
wrong_img = tk.PhotoImage(file="images/wrong.png")
correct_btn = tk.Button(
image=correct_img,
bd=0,
relief="flat",
background=BACKGROUND_COLOR,
activebackground=BACKGROUND_COLOR,
highlightthickness=0,
pady=20,
command=knew_it,
)
correct_btn.grid(column=0, row=1)
wrong_btn = tk.Button(
image=wrong_img,
bd=0,
relief="flat",
background=BACKGROUND_COLOR,
activebackground=BACKGROUND_COLOR,
highlightthickness=0,
pady=20,
command=not_knew_it,
)
wrong_btn.grid(column=2, row=1)
text = tk.Label(
text="Did you get it?", font=("Arial", 20, "bold"), background=BACKGROUND_COLOR
)
text.grid(column=1, row=1)
# Data
data = pd.read_csv("data/arabic_words.csv")
arabic_words = data.Arabic
english_words = data.English
word_is_a = data["Is a _"]
id_data = data.ID
total_records = len(data) - 1
flip_timer = window.after(3000, flip_card)
get_ar_word()
# print(len(arabic_words))
# print(english_words)
# print(word_is_a)
window.mainloop()