任何人都可以帮我这个代码吗?它不起作用。
import Tkinter as t
from PIL import Image, ImageTk
import random
rockimagepath = "rock.jpg"
paperimagepath = "paper.jpg"
scissorimagepath = "scissor.jpg"
def rockandrock():
return "The computer also chose rock it's a draw!"
def rockandpaper():
return "The computer chose paper and it wins. Better luck next time!"
def rockandscissor():
return "The computer chose scissor and you win!"
def paperandpaper():
return "The computer also chose paper and it's a draw!"
def paperandscissor():
return "The computer chose scissor and it wins. Better luck next time!"
def scissorandscissor():
return "The computer also chose scissor and it's a draw!"
def paperandrock():
return "The computer chose rock and you win!"
def scissorandrock():
return "The computer chose rock and it wins. Better luck next time!"
def scissorandpaper():
return "The computer chose paper and you win!"
def whowon(item1, item2):
if item1=="rock" and item2=="rock":
return rockandrock()
elif item1=="rock" and item2=="paper":
return rockandpaper()
elif item1=="rock" and item2=="scissor":
return rockandscissor()
elif item1=="paper" and item2=="paper":
return paperandpaper()
elif item1=="paper" and item2=="scissor":
return paperandscissor()
elif item1=="scissor" and item2=="scissor":
return scissorandscissor()
elif item1=="paper" and item2=="rock":
return paperandrock()
elif item1=="scissor" and item2=="rock":
return scissorandrock()
elif item1=="scissor" and item2=="paper":
return scissorandpaper()
def rock_click():
pcrandint = random.randint(1, 3)
if pcrandint == 1:
pcoption = "rock"
elif pcrandint == 2:
pcoption = "paper"
elif pcrandint == 3:
pcoption = "scissor"
output = whowon(rock, pcoption)
winna = t.Label(text=output).pack()
def paper_click():
pcrandint = random.randint(1, 3)
if pcrandint == 1:
pcoption = "rock"
elif pcrandit == 2:
pcoption = "paper"
elif pcrandint == 3:
pcoption = "scissor"
whowon(paper, pcoption)
def scissor_click():
pcrandint = random.randint(1, 3)
if pcrandint == 1:
pcoption = "rock"
elif pcrandit == 2:
pcoption = "paper"
elif pcrandint == 3:
pcoption = "scissor"
whowon(scissor, pcoption)
maingui = t.Tk()
rockphoto = t.PhotoImage(file="rock.gif")
paperphoto = t.PhotoImage(file="paper.gif")
scissorphoto = t.PhotoImage(file="scissor.gif")
rock = t.Button(maingui, image=rockphoto, command=rock_click).pack()
paper = t.Button(maingui, image=paperphoto, command=paper_click).pack()
scissor = t.Button(maingui, image=scissorphoto, command=scissor_click).pack()
问题在于胜利者没有出现。我只是想在python中创建一个tkinter rock paper剪刀游戏。我不知道更多的细节。这只是一个给我的朋友和家人留下深刻印象的练习项目。
答案 0 :(得分:1)
你还没有启动主循环,所以把它放在最后:
maingui.mainloop()
答案 1 :(得分:1)
这里有很多东西。
maingui.mainloop()
。我怀疑你忘了把它包括在内。*_click()
方法中,您将rock
作为参数传递,实际上是将您的摇滚按钮作为对象从顶级上下文传递。您需要将字符串'rock'
作为参数传递。 pcrandit
应为pcrandint
在代码的最后添加:
winner = t.Label(maingui)
winner.pack()
然后,当您想要编辑标签时,请使用:
winner['text'] = whowon(...)
应该得到你想要的东西。顺便说一下,当你这样做时:
rock = t.Button(maingui, image=rockphoto, command=rock_click).pack()
您实际上并没有获得对该按钮的引用。它会创建按钮,但会将rock分配给.pack()
答案 2 :(得分:0)
您可以大大减少代码的逻辑:
def tie(item):
return "The computer also chose %s it's a draw!" % item
def pc_won(item):
return "The computer chose %s and it wins. Better luck next time!" % item
def you_won(item):
return "The computer chose %s and you win!" % item
def who_won(you):
pc = random.randint(0, 2)
choices = ['rock', 'paper', 'scissor']
you = choices.index(you)
if you == pc:
return tie(choices[pc])
flip = you > pc
if (flip + you - pc) % 2:
return pc_won(choices[pc])
else:
return you_won(choices[pc])
#your code was not consistent for this part
def rock_click():
output = who_won('rock')
winna = t.Label(text=output).pack()
def paper_click():
who_won('paper')
def scissor_click():
who_won('scissor')