Skip to content

Commit 7f8f411

Browse files
committedOct 17, 2023
added Flashcard GUI
1 parent ce93fe5 commit 7f8f411

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed
 

‎F/FlashCard App/main.py

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Import required libraries
2+
from tkinter import *
3+
import pandas
4+
import random
5+
6+
# Define background color
7+
BACKGROUND_COLOR = "#B1DDC6"
8+
9+
# Initialize variables
10+
current_card = {}
11+
to_learn = {}
12+
13+
# Attempt to read the data from a CSV file
14+
try:
15+
data = pandas.read_csv("data\words_to_learn.csv")
16+
except FileNotFoundError:
17+
# If the file is not found, load data from a different CSV
18+
original_data = pandas.read_csv("F\FlashCard App\data\hindi_words.csv")
19+
to_learn = original_data.to_dict(orient="records")
20+
else:
21+
# If data is successfully loaded, convert it to a list of dictionaries
22+
to_learn = data.to_dict(orient="records")
23+
24+
# Function to display the next flashcard
25+
def next_card():
26+
global current_card, flip_timer
27+
window.after_cancel(flip_timer)
28+
current_card = random.choice(to_learn)
29+
canvas.itemconfig(card_title, text="Hindi", fill="black")
30+
canvas.itemconfig(card_word, text=current_card["Hindi"], fill="black")
31+
canvas.itemconfig(card_bg, image=card_front_img)
32+
flip_timer = window.after(3000, func=flip_card)
33+
34+
# Function to flip the flashcard to reveal the English translation
35+
def flip_card():
36+
canvas.itemconfig(card_title, text="English", fill="white")
37+
canvas.itemconfig(card_word, text=current_card["English"], fill="white")
38+
canvas.itemconfig(card_bg, image=card_back_img)
39+
40+
# Function to mark a card as known and remove it from the to-learn list
41+
def is_known():
42+
to_learn.remove(current_card)
43+
data = pandas.DataFrame(to_learn)
44+
data.to_csv("F/Flashcard App/data/words_to_learn.csv", index=False)
45+
next_card()
46+
47+
# Create the main window
48+
window = Tk()
49+
window.title("Flashy App")
50+
window.config(padx=50, pady=50, bg=BACKGROUND_COLOR)
51+
flip_timer = window.after(3000, func=flip_card)
52+
53+
# Create a canvas for displaying the flashcards
54+
canvas = Canvas(width=800, height=526)
55+
card_front_img = PhotoImage(file="F\FlashCard App\images\card_front.png")
56+
card_back_img = PhotoImage(file="F\FlashCard App\images\card_back.png")
57+
card_bg = canvas.create_image(400, 263, image=card_front_img)
58+
canvas.config(bg=BACKGROUND_COLOR, highlightthickness=0)
59+
card_title = canvas.create_text(400, 150, text="Title", font=("Arial", 68, "italic"))
60+
card_word = canvas.create_text(400, 263, text="word", font=("Arial", 60, "bold"))
61+
canvas.grid(row=0, column=0, columnspan=2)
62+
63+
# Create buttons for "unknown" and "known" flashcards
64+
cross_img = PhotoImage(file="F\FlashCard App\images\wrong.png")
65+
unknown_button = Button(image=cross_img, highlightthickness=0, command=next_card)
66+
unknown_button.grid(row=1, column=0)
67+
68+
check_image = PhotoImage(file="F/FlashCard App/images/right.png")
69+
known_button = Button(image=check_image, highlightthickness=0, command=is_known)
70+
known_button.grid(row=1, column=1)
71+
72+
# Display the first flashcard
73+
next_card()
74+
75+
# Start the main GUI loop
76+
window.mainloop()

0 commit comments

Comments
 (0)