The code is as follows:
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()
There is a restriction that only labels should be used without using canvas. However, when I turn this code, the error does not appear, so I don't know what to fix, and I don't know why it went wrong.
Help me.
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()
© 2024 OneMinuteCode. All rights reserved.