tkinter로 가위바위보 프로그램을 만드는 중인데 에러도 안 뜨고 원하는 결과물도 나오지 않습니다.
조회수 1699회
코드는 다음과 같습니다.
from tkinter import *
import random
window = Tk()
button_list=['Rock', 'Paper', 'Scissors']
RockImage=PhotoImage(file="rock.png")
PaperImage=PhotoImage(file="paper.png")
ScissorImage=PhotoImage(file="scissors.png")
def update_image(num,Img):
if num==1:
inputLabel_1=Label(window, image=Img)
inputLabel_1.grid(row=0, column=0)
elif num==2:
inputLabel_2=Label(window, image=Img)
inputLabel_2.grid(row=0, column=2)
Mid_ResultLabel=Label(window, text=' ', fg='green')
ResultLabel=Label(window, text=' ',fg='green')
Mid_ResultLabel.grid(row=0, column=1)
ResultLabel.grid(row=1, column=1)
def game(choice):
opponent = random.randint(1,3)
if opponent == 1:
update_image(2,RockImage)
elif opponent == 2:
update_image(2,PaperImage)
elif opponent ==3:
update_image(2,ScissorImage)
if choice == 'Rock':
update_image(1,RockImage)
if opponent == 1:
Mid_ResultLabel = Label(window, width=10, text='======')
ResultLabel = Label(window, width=10, text='DRAW!',fg='green')
elif opponent == 2:
Mid_ResultLabel = Label(window, width=10, text='<<<<<<')
ResultLabel = Label(window, width=10, text='LOSE...')
elif opponent ==3:
Mid_ResultLabel = Label(window, width=10, text='>>>>>>')
ResultLabel = Label(window, width=10, text='YOU WON!')
elif choice == 'Paper':
update_image(1,PaperImage)
if opponent == 1:
Mid_ResultLabel = Label(window, width=10, text='>>>>>>')
ResultLabel = Label(window, width=10, text='YOU WON!')
elif opponent == 2:
Mid_ResultLabel = Label(window, width=10, text='======')
ResultLabel = Label(window, width=10, text='DRAW!')
elif opponent == 3:
Mid_ResultLabel = Label(window, width=10, text='<<<<<<')
ResultLabel = Label(window, width=10, text='LOSE...')
elif choice == 'Scissors':
update_image(1,ScissorImage)
if opponent == 1:
Mid_ResultLabel = Label(window, width=10, text='<<<<<<')
ResultLabel = Label(window, width=10, text='LOSE...')
elif opponent == 2:
Mid_ResultLabel = Label(window, width=10, text='>>>>>>')
ResultLabel = Label(window, width=10, text='YOU WON!')
elif opponent == 3 :
Mid_ResultLabel = Label(window, width=10, text='======')
ResultLabel = Label(window, width=10, text='DRAW!')
Mid_ResultLabel.grid(row=0, column=1)
ResultLabel.grid(row=1, column=1)
i=0
for button_text in button_list:
def click(t=i):
game(t)
Button(window, text=button_text, width=30, command = click).grid(row=3, column = i)
i+=1
window.mainloop()
캔버스를 사용하지 않고 라벨만을 이용해야한다는 제약이 있습니다. 하지만 이 코드을 돌리면 에러도 뜨지 않아서 어떤 부분을 고쳐야 할지도 감이 안오고, 왜 잘못되었는지도 전혀 감이 오질 않습니다.
도와주세요.
-
(•́ ✖ •̀)
알 수 없는 사용자 - 〉
1 답변
-
- 이미지 파일 위치와 실행위치가 달라서 파일을 못찾아서 문제가 생겼을 가능성이 커 보이고요.
game
의choice
인자가 구현부에는 문자열로, 호출부에는 숫자인덱스로 되어 있어서, 서로 맞지 않았고요.- 결과를
grid
에 붙이는 부분이 들여쓰기가 잘못되어 있었던 것 같구요.
import os from tkinter import * import random window = Tk() button_list = ["Rock", "Paper", "Scissors"] dirname = os.path.dirname(os.path.abspath(__file__)) RockImage = PhotoImage(file=os.path.join(dirname, "rock.png")) PaperImage = PhotoImage(file=os.path.join(dirname, "paper.png")) ScissorImage = PhotoImage(file=os.path.join(dirname, "scissors.png")) def update_image(num, Img): if num == 1: inputLabel_1 = Label(window, image=Img) inputLabel_1.grid(row=0, column=0) elif num == 2: inputLabel_2 = Label(window, image=Img) inputLabel_2.grid(row=0, column=2) def game(choice): opponent = random.randint(1, 3) if opponent == 1: update_image(2, RockImage) elif opponent == 2: update_image(2, PaperImage) elif opponent == 3: update_image(2, ScissorImage) if choice == 1: update_image(1, RockImage) elif choice == 2: update_image(1, PaperImage) elif choice == 3: update_image(1, ScissorImage) if (choice, opponent) in [(1, 2), (2, 3), (3, 1)]: Mid_ResultLabel = Label(window, width=10, text="<<<<<<", fg="red") ResultLabel = Label(window, width=10, text="LOSE...", fg="red") result = 1 elif (choice, opponent) in [(1, 3), (2, 1), (3, 2)]: Mid_ResultLabel = Label(window, width=10, text=">>>>>>", fg="green") ResultLabel = Label(window, width=10, text="YOU WON!", fg="green") result = -1 else: Mid_ResultLabel = Label(window, width=10, text="======") ResultLabel = Label(window, width=10, text="DRAW!") result = 0 Mid_ResultLabel.grid(row=0, column=1) ResultLabel.grid(row=1, column=1) return result for i, button_text in enumerate(button_list): def click(t=i): game(t + 1) Button(window, text=button_text, width=30, command=click).grid(row=3, column=i) window.mainloop()
댓글 입력