Python 试图编码';石头剪刀';与特金特的比赛。但是我的编码没有';我没有给出我想要的,而且没有发生错误
这就是我刚才做的Python 试图编码';石头剪刀';与特金特的比赛。但是我的编码没有';我没有给出我想要的,而且没有发生错误,python,tkinter,Python,Tkinter,这就是我刚才做的 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,
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()
从tkinter导入*
随机输入
window=Tk()
按钮列表=[“石头”、“布”、“剪刀”]
RockImage=PhotoImage(file=“rock.png”)
PaperImage=PhotoImage(file=“paper.png”)
剪刀图像=照片图像(file=“scissors.png”)
def update_图像(数字,Img):
如果num==1:
inputLabel_1=标签(窗口,图像=Img)
inputLabel_1.grid(行=0,列=0)
elif num==2:
inputLabel_2=标签(窗口,图像=Img)
inputLabel_2.grid(行=0,列=2)
Mid_ResultLabel=标签(窗口,文本='',前景=''绿色)
ResultLabel=Label(窗口,文本=“”,前景=“”,绿色)
Mid_ResultLabel.grid(行=0,列=1)
ResultLabel.grid(行=1,列=1)
def游戏(选择):
对手=random.randint(1,3)
如果对手==1:
更新_图像(2,RockImage)
elif对手==2:
更新_图像(2,纸质图像)
elif对手==3:
更新_图像(2,剪刀图像)
如果选项==“摇滚乐”:
更新_图像(1,RockImage)
如果对手==1:
Mid_ResultLabel=标签(窗口,宽度=10,文本='==')
ResultLabel=Label(窗口,宽度=10,text='DRAW!',fg='green')
elif对手==2:
Mid_ResultLabel=标签(窗口,宽度=10,文本=“”)
ResultLabel=Label(窗口,宽度=10,文本=“您赢了!”)
elif选项==‘纸张’:
更新图片(1,纸质图片)
如果对手==1:
Mid_ResultLabel=标签(窗口,宽度=10,文本='>>>>)
ResultLabel=Label(窗口,宽度=10,文本=“您赢了!”)
elif对手==2:
Mid_ResultLabel=标签(窗口,宽度=10,文本='==')
ResultLabel=Label(窗口,宽度=10,text='DRAW!')
elif对手==3:
Mid_ResultLabel=Label(window,width=10,text='首先,您向game
函数传递一个整数,而不是字符串,因此当您检查它是石头、布还是剪刀时,它永远不会返回值。这意味着您应该将代码更改为:
if choice=='Rock':->if choice==0:
elif选项==‘纸张’:->elif选项==1:
elif选项==‘剪刀’:->elif选项==2:
此外,您可以使用label.configure
来更改文本,而不是重建标签:
如果选项==0:
更新_图像(1,RockImage)
如果对手==1:
Mid_ResultLabel.configure(text='====')
ResultLabel.configure(text='DRAW!')
elif对手==2:
Mid_ResultLabel.configure(text='What's mist?Your image not should?'def game()中):',对手的照片出现了,但我的没有…但有人给了我更正!我刚读了你的回答。这是我上传到这里的第一个问题,你是我遇到的第一个导师。非常感谢。你解决了我的好奇心。但我仍然不理解“你传递给游戏函数的是整数而不是字符串”.为什么我应该写012而不是RockPaperScissor?@Jason这是因为你发送变量I
而不是变量button\u text
,如果你发送变量button\u text
,你可以使用if==“rock”等