learning from 100 Days of Code: The Complete Python Pro Bootcamp for 2022
Flash Card Project
new_word()
def new_word():
global current_card, flip_timer
window.after_cancel(flip_timer)
current_card = choice(to_learn) # 隨機選擇dictionary的內容
canvas.itemconfig(card_img, image=card_front)
canvas.itemconfig(card_title, text="English", fill="black")
canvas.itemconfig(card_word, text=current_card["English"], fill="black")
flip_timer = window.after(3000, flip_card)
is_learned()
def is_learned():
to_learn.remove(current_card)
pandas.DataFrame(to_learn).to_csv("data/word_to_learn.csv", index=False)
new_word()
flip_card()
ef flip_card():
canvas.itemconfig(card_img, image=card_back)
canvas.itemconfig(card_title, text="Chinese", fill="white")
canvas.itemconfig(card_word, text=current_card["Chinese"], fill="white")
Main
from tkinter import *
from random import choice
import pandas
BACKGROUND_COLOR = "#B1DDC6"
to_learn = {}
try:
data = pandas.read_csv("data/word_to_learn.csv")
except FileNotFoundError:
original_data = pandas.read_csv("data/english_word.csv")
to_learn = original_data.to_dict("records")
else:
to_learn = data.to_dict("records")
current_card = {}
window = Tk()
window.title("Flash Card Game")
window.config(bg=BACKGROUND_COLOR, padx=50, pady=50)
flip_timer = window.after(3000, flip_card)
canvas = Canvas(bg=BACKGROUND_COLOR, width=800, height=526, highlightthickness=0)
card_back = PhotoImage(file="images/card_back.png")
card_front = PhotoImage(file="images/card_front.png")
card_img = canvas.create_image(400, 265, image=card_front)
card_title = canvas.create_text(400, 150, fill="black", font=("Arial", 40, "italic"))
card_word = canvas.create_text(400, 263, fill="black", font=("Arial", 60, "bold"))
canvas.grid(row=1, column=1, columnspan=2)
right = PhotoImage(file="images/right.png")
wrong = PhotoImage(file="images/wrong.png")
right_button = Button(image=right, highlightthickness=0, command=is_learned)
wrong_button = Button(image=wrong, highlightthickness=0, command=new_word)
right_button.grid(row=2, column=2)
wrong_button.grid(row=2, column=1)
new_word()
window.mainloop()