首页 > 解决方案 > 尝试用 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,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()

我不能在这件事上使用画布..只允许使用标签。运行此程序时不会出现错误。所以我无法弄清楚出了什么问题。

我应该在这里编辑什么?我在哪里犯了错误?

标签: pythontkinter

解决方案


首先,您将整数而不是字符串传递给game函数,因此当您检查它是石头、纸还是剪刀时,它永远不会返回值。这意味着您应该将代码更改为:

if choice == 'Rock': -> if choice == 0:
elif choice == 'Paper': -> elif choice == 1:
elif choice == 'Scissors': -> elif choice == 2:

label.configure此外,您可以使用简单地更改文本,而不是重建标签:

if choice == 0:
    update_image(1,RockImage)
    if opponent == 1:
        Mid_ResultLabel.configure(text='======')
        ResultLabel.configure(text='DRAW!')
    elif opponent == 2:
        Mid_ResultLabel.configure(text='<<<<<<')
        ResultLabel.configure(text='LOSE...')
    elif opponent ==3:
        Mid_ResultLabel.configure(text=">>>>>>")
        ResultLabel.configure(text='YOU WON!')

elif choice == 1:
    update_image(1,PaperImage)
    if opponent == 1:
        Mid_ResultLabel.configure(text=">>>>>>")
        ResultLabel.configure(text='YOU WON!')
    elif opponent == 2:
        Mid_ResultLabel.configure(text='======')
        ResultLabel.configure(text='DRAW!')
    elif opponent == 3:
        Mid_ResultLabel.configure(text='<<<<<<')
        ResultLabel.configure(text='LOSE...')

elif choice == 2:
    update_image(1,ScissorImage)
    if opponent == 1:
        Mid_ResultLabel.configure(text='<<<<<<')
        ResultLabel.configure(text='LOSE...')
    elif opponent == 2:
        Mid_ResultLabel.configure(text=">>>>>>")
        ResultLabel.configure(text='YOU WON!')
    elif opponent == 3 :
        Mid_ResultLabel.configure(text='======')
        ResultLabel.configure(text='DRAW!')

您还可以对代码进行更多改进;但是,这些更改将使您的代码正常工作!

总体而言,所做的代码更改

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):
    print("GAME FUNCTION")
    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 == 0:
        update_image(1,RockImage)
        if opponent == 1:
            Mid_ResultLabel.configure(text='======')
            ResultLabel.configure(text='DRAW!')
        elif opponent == 2:
            Mid_ResultLabel.configure(text='<<<<<<')
            ResultLabel.configure(text='LOSE...')
        elif opponent ==3:
            Mid_ResultLabel.configure(text=">>>>>>")
            ResultLabel.configure(text='YOU WON!')

    elif choice == 1:
        update_image(1,PaperImage)
        if opponent == 1:
            Mid_ResultLabel.configure(text=">>>>>>")
            ResultLabel.configure(text='YOU WON!')
        elif opponent == 2:
            Mid_ResultLabel.configure(text='======')
            ResultLabel.configure(text='DRAW!')
        elif opponent == 3:
            Mid_ResultLabel.configure(text='<<<<<<')
            ResultLabel.configure(text='LOSE...')

    elif choice == 2:
        update_image(1,ScissorImage)
        if opponent == 1:
            Mid_ResultLabel.configure(text='<<<<<<')
            ResultLabel.configure(text='LOSE...')
        elif opponent == 2:
            Mid_ResultLabel.configure(text=">>>>>>")
            ResultLabel.configure(text='YOU WON!')
        elif opponent == 3 :
            Mid_ResultLabel.configure(text='======')
            ResultLabel.configure(text='DRAW!')


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()

您可以进行的其他更改

您还可以通过其他方式改进您的代码,使其更易读懂。可以说最重要的是将您的代码移动到面向对象编程中,我发现本指南相当不错!

您可以进行的另一项更改是更改for loop创建按钮的 ,以使用 Python 的enumerate函数和lambda

for i, button_text in enumerate(button_list):
    Button(window, text=button_text, width=30, command = lambda t=i: game(t)).grid(row=3, column = i)

如您所见,一次代码更改可以改善代码的外观!

希望这可以帮助,

詹姆士


推荐阅读