코드는 다음과 같습니다.
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()
캔버스를 사용하지 않고 라벨만을 이용해야한다는 제약이 있습니다. 하지만 이 코드을 돌리면 에러도 뜨지 않아서 어떤 부분을 고쳐야 할지도 감이 안오고, 왜 잘못되었는지도 전혀 감이 오질 않습니다.
도와주세요.