본문 바로가기
패션프로젝트/초미니프로젝트

파이썬 미니 프로젝트 : tkinter/matplotlib/GUI/그래프를 활용한 영어 퀴즈 프로그램 만들기

by nowjin 2025. 3. 17.

시작전 프로젝트 구상

1. 데이터 즉 단어는 csv파일로 불러오기

2. 단어 순서는 무작위

3. 똑같은 단어를 2번 안불러오게 중복 X

 

 

시작시 화면
정답시와 오답시 배경화면 색
퀴즈가 끝났을때 알림
끝나고 pie그래프로 결과 표시

 

import csv
from tkinter import *
from tkinter import messagebox
from matplotlib import pyplot
from matplotlib import font_manager, rc
import random

bgColor = 'silver'  # 백그라운드 컬러
coColor = 'gold'  # 정답을 나타내는 컬러
wrColor = 'tomato'  # 오답을 나타내는 컬러
btColor = 'yellow'  # 버튼 컬러

# csv파일로 문제 불러오기
with open("eng_quiz_data.csv", 'r', encoding="UTF-8-sig") as file:
    questions = list(csv.reader(file))

repeat = 0  # 정답 체크 함수 반복 횟수
answer = 0


# 문제 생성 함수
def random_question():
    global answer
    global repeat

    repeat += 1  # 새로 문제 생성 할때마다 반복 횟수 증가
    for i in range(4):  # 다음문제로 넘어갔을때 버튼 색깔 초기화
        buttons[i].config(bg='white')

    random_choice_question = random.sample(questions, 4)  # question 리스트에서 무작위로 4개 뽑기
    answer = random.randint(0, 3)
    choiced_question = random_choice_question[answer][0]  # 4개 중에 뽑힌 1개의 문제
    questions.remove(random_choice_question[answer])  # 뽑힌 문제는 제거
    question_label.config(text=choiced_question)  # 문제 생성

    for j in range(4):
        buttons[j].config(text=random_choice_question[j][1])  # 문제 아래 버튼 4개에 무작위로 뜻 배치

    if repeat == 16:  # 총 20문제중 15문제 제출
        # test위해 5문제로 수정
        win.quit()


entry_count = len(questions)  # 전체 문제 수
correct_count = 0  # 정답 횟수
wrong_count = 0  # 오답 횟수


# 정답 체크 함수
def check_answer(index):
    global correct_count
    global wrong_count
    index = int(index)
    if answer == index:  # 정답
        buttons[index].config(bg=coColor)
        correct_count += 1  # 정답 횟수 증가
        win.after(1500, random_question)  # 답을 선택했을때 자동으로 넘어가는 기능
    else:  # 오답
        buttons[index].config(bg=wrColor)
        wrong_count += 1  # 오답 횟수 증가
        win.after(1500, random_question)  # 답을 선택했을때 자동으로 넘어가는 기능


win = Tk()
win.title('영어 퀴즈')
win.config(padx=30, pady=10, bg=bgColor)
# 문제를 나타내는 label
question_label = Label(win, width=20, height=2, text='test', font=('candara', 25, 'bold'), bg=bgColor, fg='black')
question_label.pack(pady=30)

# 버튼 4개를 담을 리스트
buttons = []
for k in range(4):
    bt = Button(win, text=f'{k}번', width=35, height=2, command=lambda index=k: check_answer(index),
                font=('candara', 15, 'bold'), bg='white')
    bt.pack()
    buttons.append(bt)

# '다음 문제' 버튼
next_bt = Button(win, text='S k i p', width=15, height=2, command=random_question, font=('candara', 15, 'bold'),
                 bg=btColor)
next_bt.pack(pady=30)

random_question()

win.mainloop()


def popup():
    messagebox.showinfo('영어 단어 퀴즈', '영어 단어 퀴즈가 종료되었습니다.')


popup()  # 메시지 박스 출력 후 그래프 보기

nullity_count = 15 - correct_count - wrong_count  # 무효 횟수
print('정답횟수:{}개'.format(correct_count))
print('오답횟수:{}개'.format(wrong_count))
print('무효횟수:{}개'.format(nullity_count))

# 차트에 한글입력시 깨지는 현상 방지하기 위해 폰트 지정
font_path = "C:/Windows/Fonts/NGULIM.TTF"
font = font_manager.FontProperties(fname=font_path).get_name()
rc('font', family=font)

label = ['정답', '틀린 문제', '모르는 문제']
pop = [correct_count, wrong_count, nullity_count]
wg = {'linewidth': 3}
group_colors = ['yellowgreen', 'lightskyblue', 'lightcoral']
pyplot.pie(pop, labels=label, autopct='%.2f%%', wedgeprops=wg, colors=group_colors)
pyplot.show()