1. 인터페이스 만들기
BACKGROUND_COLOR = "#B1DDC6"
from tkinter import *
# 1. 윈도우 생성하기
window = Tk()
window.title("StudyCard")
window.config(padx=50, pady=50, bg=BACKGROUND_COLOR)
# 2-1. 컨버스에 생성하기
canvas = Canvas(width=800, height=525, bg=BACKGROUND_COLOR, highlightthickness=0)
# 2-2. 컨버스에다가 이미지, 텍스트 넣기
bg_small_img = PhotoImage(file="./images/card_front.png")
canvas.create_image(400,265, image=bg_small_img)
canvas.create_text(150, 50, text="Let's study French! by OliviaCodingSchool", font= ("Arial", 10))
canvas.create_text(400,150, text="Nation", fill= "Purple", font = ("Arial", 30, "italic"))
canvas.create_text(400,270, text="Word", fill= "black", font = ("Arial", 60, "bold"))
canvas.grid(row=0,column=0, columnspan=2)
# 3. 버튼 두 개 넣기
o_img = PhotoImage(file="./images/right.png")
button_o = Button(image=o_img, highlightthickness=0)
button_o.grid(row=1, column=0)
x_img=PhotoImage(file="./images/wrong.png")
button_x = Button(image=x_img, highlightthickness=0)
button_x.grid(row=1, column=1)
window.mainloop()
2. 카드 뒤집기
버튼을 누르면 프랑스어가 나오고,
시간이 지나면 자동으로 영어로 번역하기
BACKGROUND_COLOR = "#B1DDC6"
from tkinter import *
import pandas
import random
# 1. 윈도우 생성하기
window = Tk()
window.title("StudyCard")
window.config(padx=50, pady=50, bg=BACKGROUND_COLOR)
# 데이터파일
data = pandas.read_csv("data/french_words.csv")
french_list = data.to_dict("records")
print(french_list)
one_french_word = ''
def new_word():
global french_list
global one_french_word
global timer
window.after_cancel(timer)
canvas.itemconfig(background_img, image= front_img)
one_french_word = random.choice(french_list)
french_list.remove(one_french_word)
canvas.itemconfig(word_text, text= one_french_word["French"], fill ="black")
canvas.itemconfig(nation_text, text="French", fill = "black")
timer = window.after(3000, change_word)
# 단어바꾸기
def change_word():
canvas.itemconfig(background_img, image=back_img)
canvas.itemconfig(word_text, text = one_french_word["English"], fill="white")
canvas.itemconfig(nation_text, text= "English", fill="white", font = ("Arial", 30, "italic"))
timer = window.after(1000, change_word)
# 2-1. 컨버스에 생성하기
canvas = Canvas(width=800, height=525, bg=BACKGROUND_COLOR, highlightthickness=0)
# 2-2. 컨버스에다가 이미지, 텍스트 넣기
back_img = PhotoImage(file="images/card_back.png")
front_img = PhotoImage(file="./images/card_front.png")
background_img= canvas.create_image(400,265, image=front_img)
canvas.create_text(150, 50, text="Let's study French! by OliviaCodingSchool", font= ("Arial", 10))
nation_text = canvas.create_text(400,150, text="French", fill= "Purple", font = ("Arial", 30, "italic"))
word_text = canvas.create_text(400,270, text="Word", fill= "black", font = ("Arial", 60, "bold"))
canvas.grid(row=0,column=0, columnspan=3)
# 3. 버튼 두 개 넣기
o_img = PhotoImage(file="./images/o_img.png")
button_o = Button(image=o_img, highlightthickness=0, bg=BACKGROUND_COLOR, command=new_word)
button_o.grid(row=1, column=0)
x_img=PhotoImage(file="./images/x_img.png")
button_x = Button(image=x_img, highlightthickness=0, bg=BACKGROUND_COLOR, command=new_word)
button_x.grid(row=1, column=2)
# 새로운 카드 보여주기
new_word()
window.mainloop()
3. 파일 저장하기(V단어는 암기 리스트에서 제외하기) / 예외 잡기
BACKGROUND_COLOR = "#B1DDC6"
from tkinter import *
import pandas
import random
# 1. 윈도우 생성하기
window = Tk()
window.title("StudyCard")
window.config(padx=50, pady=50, bg=BACKGROUND_COLOR)
# 데이터파일
try:
data = pandas.read_csv("save_data.csv")
except FileNotFoundError:
original_data = pandas.read_csv("data/french_words.csv")
french_list = original_data.to_dict("records")
else:
french_list = data.to_dict("records")
one_french_word = ''
def known_word():
global french_list
global one_french_word
global timer
window.after_cancel(timer)
canvas.itemconfig(background_img, image= front_img)
one_french_word = random.choice(french_list)
french_list.remove(one_french_word)
print(french_list)
canvas.itemconfig(word_text, text= one_french_word["French"], fill ="black")
canvas.itemconfig(nation_text, text="French", fill = "black")
timer = window.after(3000, change_word)
save_to_csv()
def retry_word():
global french_list
global one_french_word
global timer
window.after_cancel(timer)
canvas.itemconfig(background_img, image= front_img)
one_french_word = random.choice(french_list)
print(french_list)
canvas.itemconfig(word_text, text= one_french_word["French"], fill ="black")
canvas.itemconfig(nation_text, text="French", fill = "black")
timer = window.after(3000, change_word)
save_to_csv()
# 단어바꾸기
def change_word():
canvas.itemconfig(background_img, image=back_img)
canvas.itemconfig(word_text, text = one_french_word["English"], fill="white")
canvas.itemconfig(nation_text, text= "English", fill="white", font = ("Arial", 30, "italic"))
def save_to_csv():
with open(file="save_data.csv", mode="w") as f:
new_data = pandas.DataFrame(french_list)
f.write(new_data.to_csv())
timer = window.after(1000, change_word)
# 2-1. 컨버스에 생성하기
canvas = Canvas(width=800, height=525, bg=BACKGROUND_COLOR, highlightthickness=0)
# 2-2. 컨버스에다가 이미지, 텍스트 넣기
back_img = PhotoImage(file="images/card_back.png")
front_img = PhotoImage(file="./images/card_front.png")
background_img= canvas.create_image(400,265, image=front_img)
canvas.create_text(150, 50, text="Let's study French! by OliviaCodingSchool", font= ("Arial", 10))
nation_text = canvas.create_text(400,150, text="French", fill= "Purple", font = ("Arial", 30, "italic"))
word_text = canvas.create_text(400,270, text="Word", fill= "black", font = ("Arial", 60, "bold"))
canvas.grid(row=0,column=0, columnspan=3)
# 3. 버튼 두 개 넣기
o_img = PhotoImage(file="./images/o_img.png")
button_o = Button(image=o_img, highlightthickness=0, bg=BACKGROUND_COLOR, command=known_word)
button_o.grid(row=1, column=0)
x_img=PhotoImage(file="./images/x_img.png")
button_x = Button(image=x_img, highlightthickness=0, bg=BACKGROUND_COLOR, command=retry_word)
button_x.grid(row=1, column=2)
# 시작하기
try:
retry_word()
except IndexError:
original_data = pandas.read_csv("data/french_words.csv")
french_list = original_data.to_dict("records")
retry_word()
window.mainloop()
또는
BACKGROUND_COLOR = "#B1DDC6"
from tkinter import *
import pandas
import random
# 1. 윈도우 생성하기
window = Tk()
window.title("StudyCard")
window.config(padx=50, pady=50, bg=BACKGROUND_COLOR)
# 데이터파일
try:
data = pandas.read_csv("save_data.csv")
except:
original_data = pandas.read_csv("data/french_words.csv")
french_list = original_data.to_dict("records")
else:
french_list = data.to_dict("records")
one_french_word = ''
def next_word():
global french_list
global one_french_word
global timer
window.after_cancel(timer)
canvas.itemconfig(background_img, image= front_img)
one_french_word = random.choice(french_list)
print(french_list)
canvas.itemconfig(word_text, text= one_french_word["French"], fill ="black")
canvas.itemconfig(nation_text, text="French", fill = "black")
timer = window.after(3000, change_word)
def known_word():
french_list.remove(one_french_word)
new_data = pandas.DataFrame(french_list)
new_data.to_csv("save_data.csv", index=False)
next_word()
# 단어바꾸기
def change_word():
canvas.itemconfig(background_img, image=back_img)
canvas.itemconfig(word_text, text = one_french_word["English"], fill="white")
canvas.itemconfig(nation_text, text= "English", fill="white", font = ("Arial", 30, "italic"))
timer = window.after(1000, change_word)
# 2-1. 컨버스에 생성하기
canvas = Canvas(width=800, height=525, bg=BACKGROUND_COLOR, highlightthickness=0)
# 2-2. 컨버스에다가 이미지, 텍스트 넣기
back_img = PhotoImage(file="images/card_back.png")
front_img = PhotoImage(file="./images/card_front.png")
background_img= canvas.create_image(400,265, image=front_img)
canvas.create_text(150, 50, text="Let's study French! by OliviaCodingSchool", font= ("Arial", 10))
nation_text = canvas.create_text(400,150, text="French", fill= "Purple", font = ("Arial", 30, "italic"))
word_text = canvas.create_text(400,270, text="Word", fill= "black", font = ("Arial", 60, "bold"))
canvas.grid(row=0,column=0, columnspan=3)
# 3. 버튼 두 개 넣기
o_img = PhotoImage(file="./images/o_img.png")
button_o = Button(image=o_img, highlightthickness=0, bg=BACKGROUND_COLOR, command=known_word)
button_o.grid(row=1, column=0)
x_img=PhotoImage(file="./images/x_img.png")
button_x = Button(image=x_img, highlightthickness=0, bg=BACKGROUND_COLOR, command=next_word)
button_x.grid(row=1, column=2)
# 시작하기
try:
next_word()
except IndexError:
original_data = pandas.read_csv("data/french_words.csv")
french_list = original_data.to_dict("records")
next_word()
window.mainloop()
'파이썬 > 파이썬(python) 중급' 카테고리의 다른 글
[32-2 파이썬] datetime 모듈, now(), weekday(), 요일 (1) | 2022.09.25 |
---|---|
[32-1 파이썬] 구글 SMTP 포트 (1) | 2022.09.25 |
[31-3 파이썬] pandas, to_dict(), orient, record (1) | 2022.09.24 |
[31-2 파이썬] tkinter 위치, 정렬 anchor, justify (0) | 2022.09.24 |
[31-1 파이썬] 언어별 자주 쓰이는 단어 모음 frequency lists (1) | 2022.09.20 |