Skip to content
파이썬
2023.10.04 23:33

파이썬 랜덤으로 문제풀기 #2

조회 수 75458 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

파이썬 랜덤으로 문제풀기 #2

이번에는 숫자를 입력하는 방식이 아닌 라디오버튼 선택 후 확인버튼을 눌러 정답을 맞추는 방식입니다.

 

 

 

 


import os
import random
from PIL import Image, ImageTk
import tkinter as tk
import pygame
 
# 이미지 폴더 경로
image_folder = r'E:\python\py_code\공\data\과학'
 
# 이미지 파일 목록 생성
image_files = [f for f in os.listdir(image_folder) if f.endswith('.png')]
 
# 하단 GUI 요소를 미리 생성
window = tk.Tk()
window.title("문제 맞추기 게임")
 
# 이미지 표시용 라벨
image_label = tk.Label(window)
image_label.pack(pady=10)
 
# 이미지와 선택 버튼을 포함하는 프레임 생성
frame = tk.Frame(window)
frame.pack()
 
question_label = tk.Label(frame, text="")
question_label.grid(row=0, column=0)
 
# 정답 선택을 위한 라디오 버튼 변수
selected_answer = tk.IntVar()
 
# 정답 선택 라디오 버튼 생성
for i in range(1, 6):
    answer_radio = tk.Radiobutton(frame, text=str(i), variable=selected_answer, value=i)
    answer_radio.grid(row=0, column=i, padx=5)
 
result_label = tk.Label(frame, text="")
result_label.grid(row=1, columnspan=6)
 
# Pygame 초기화 및 MP3 파일 경로 설정
pygame.init()
correct_sound = pygame.mixer.Sound(r'E:\python\py_code\공\ok.mp3')
incorrect_sound = pygame.mixer.Sound(r'E:\python\py_code\공\nono.mp3')
 
def play_correct_sound():
    correct_sound.play()
 
def play_incorrect_sound():
    incorrect_sound.play()
 
def show_random_question():
    # 랜덤하게 이미지 선택
    random_image = random.choice(image_files)
    image_path = os.path.join(image_folder, random_image)
   
    # 이미지 표시
    img = Image.open(image_path)
    img = img.resize((400, 400))
    img = ImageTk.PhotoImage(img)
    image_label.config(image=img)
    image_label.image = img
   
    # 정답 추출
    answer = int(random_image.split('-')[1].split('.')[0])
   
    # 사용자로부터 정답 입력 받기
    def check_answer():
        user_answer = selected_answer.get()
        if user_answer == answer:
            result_label.config(text="정답을 맞췄어요.")
            play_correct_sound()  # 정답 소리 재생
            show_random_question()
        else:
            result_label.config(text="다시 한번 풀어보세요.")
            play_incorrect_sound()  # 오답 소리 재생
   
    # 하단 GUI 업데이트
    selected_answer.set(0)
    result_label.config(text="")
   
    # 하단 GUI 생성
    question_label.config(text="이미지에 나타난 숫자는 무엇일까요?")
   
    # 확인 버튼 생성 (오른쪽에 위치)
    check_button = tk.Button(frame, text="확인", command=check_answer)
    check_button.grid(row=0, column=6)
 
# 초기 문제 표시
show_random_question()
 
# Tkinter 이벤트 루프 시작
window.mainloop()
 
# pygame 종료
pygame.mixer.quit()

 

로그인 후 댓글쓰기가 가능합니다.

?

  1. 07
    Feb 2011
    08:38

    프로세스

    Category프로세스 Views312175
    Read More
  2. 23
    Jan 2013
    16:16

    윈도우 DLL 오류 해결방법

    CategoryWindowsTip Views192702
    Read More
  3. 10
    Oct 2010
    23:49

    자바스크립트로 전송(submit) 버튼 누르기

    Category컴퓨터잡담 Views103644
    Read More
  4. 24
    Aug 2010
    08:03

    hMailServer - 설치시 주의 핵심사항

    Category컴퓨터잡담 Views103068
    Read More
  5. 30
    Oct 2010
    08:15

    북마크 링크 주소모음

    Category컴퓨터잡담 Views102936
    Read More
  6. 15
    Sep 2023
    14:04

    파이썬에서 인식이 잘되는 OCR 종류

    Category파이썬 Views76036
    Read More
  7. 29
    Sep 2023
    20:32

    파이썬 request, beautifulshop으로 정액정보 받아오기

    Category컴퓨터잡담 Views75479
    Read More
  8. 04
    Oct 2023
    23:33

    파이썬 랜덤으로 문제풀기 #2

    Category파이썬 Views75458
    Read More
  9. 14
    Sep 2023
    22:34

    한우경매낙찰 유튜브 영상의 이미지에서 특정 문자 가져와서 저장하기

    Category파이썬 Views75426
    Read More
  10. 17
    Sep 2023
    12:36

    CANON PRINTER ERROR CODE B203, B204 해결방법

    Category컴퓨터잡담 Views74907
    Read More
  11. 04
    Oct 2023
    23:29

    파이썬 랜덤으로 시험문제 풀기

    Category파이썬 Views71975
    Read More
  12. 28
    Jul 2010
    08:39

    엑셀 색깔 지정 함수

    Category컴퓨터잡담 Views65609
    Read More
  13. 21
    Nov 2009
    12:27

    MYSQL 미 해결 과제 : Can't connect to MySQL server on 'localhost'(10055)

    Category컴퓨터잡담 Views64231
    Read More
  14. 13
    Nov 2023
    05:45

    파이썬 requestsbeautifulsoup 으로 웹 input에 입력값 대입한 뒤 결과값 파일로 저장하기

    Category파이썬 Views63445
    Read More
  15. 17
    Oct 2009
    08:20

    php로 이미지를 mysql디비 저장하고 보여주는 소스

    Category컴퓨터잡담 Views62322
    Read More
  16. 22
    Jun 2010
    17:27

    여러개의 엑셀파일을 하나로 합치기

    Category컴퓨터잡담 Views57409
    Read More
  17. 25
    Sep 2010
    16:09

    오류 socket error #10061 connection

    Category컴퓨터잡담 Views54073
    Read More
  18. 13
    Dec 2023
    09:40

    지정한 이미지파일명을 출력 시키는 시험문제풀이 html

    CategoryHTMLPHPMSQL Views53862
    Read More
  19. 12
    May 2012
    17:06

    AHK) AUTOKEY 웹페이지 열지않고 소스 가져오기 또는 로그인 하기

    CategoryAutoHotKey Views52952
    Read More
  20. 21
    Sep 2013
    16:00

    VBS) VBScript Telnet log save

    CategoryVisual C++ Views51930
    Read More
Board Pagination Prev 1 2 3 4 5 ... 46 Next
/ 46

http://urin79.com

우린친구블로그

sketchbook5, 스케치북5

sketchbook5, 스케치북5

나눔글꼴 설치 안내


이 PC에는 나눔글꼴이 설치되어 있지 않습니다.

이 사이트를 나눔글꼴로 보기 위해서는
나눔글꼴을 설치해야 합니다.

설치 취소