Home > database >  Tkinter: if statements not working when clicking a button
Tkinter: if statements not working when clicking a button

Time:10-13

I'm trying to make a game of rock, paper and scissors using Tkinter, but the buttons I created neither update who the winner was nor the score.

from tkinter import *
from random import randint

root = Tk()
score = 0

scorel = Label(root, text= "Your score is: "   str(score)   "!")
scorel.grid(row=0, column=1)

move = "none"
moves = ["Rock", "Paper", "Scissors"]

def rock():
    move = "Rock"
    return move

def paper():
    move = "Paper"
    return move

def scissors():
    move = "Scissors"
    return move

cmove = "none"
resultw = Label(root, text="No winners")
resultw.grid(row=2, column=1)

def game():
    global score
    global clabel
    resultw.destroy()
    cmove = moves[randint(0, 2)]
    if move == cmove:
        resultw.config(text="It's a tie!")
    if move == "Rock":
        if cmove == "Scissors":
            resultw.config(text="You win!")
            score  = 1
        else:
            resultw.config(text="You lose!")
            score -= 1
    if move == "Paper":
        if cmove == "Rock":
            resultw.config(text="You win!")
            score  = 1
        else:
            resultw.config(text="You lose!")
            score -= 1
    if move == "Scissors":
        if cmove == "Paper":
            resultw.config(text="You win!")
            score  = 1
        else:
            resultw.config(text="You lose!")

rockb = Button(root, text="Rock", command=lambda: [rock(), game()])
rockb.grid(row=3, column=0, ipadx=30, ipady=20)

paperb = Button(root, text="Paper", command=lambda: [paper(), game()])
paperb.grid(row=3, column=1, ipadx=30, ipady=20)

scissorsb = Button(root, text="Scissors", command=lambda: [scissors(), game()])
scissorsb.grid(row=3, column=2, ipadx=30, ipady=20)


root.mainloop()

I tried calling move and cmove as global, defining them in the rock/paper/scissors function but none of it made any differcence, while

    clabel = Label(root, text="Enemy move is: "   cmove   "!")
    clabel.grid(row=1, column=2)

that I have inside my original code in game function (before the if statements) does work correctly.

CodePudding user response:

I believe we can do this with much simpler code, and without making move a global nor using a lambda:

from tkinter import *
from random import choice

beats = {"Rock": "Scissors", "Paper": "Rock", "Scissors": "Paper"}

def rock():
    game("Rock")

def paper():
    game("Paper")

def scissors():
    game("Scissors")

score = 0

def game(move):
    global score

    cmove = choice(list(beats))

    if move == cmove:
        resultw.config(text="I also chose "   cmove   ", it's a tie!")
    elif beats[move] == cmove:
        resultw.config(text="I chose "   cmove   ", you win!")
        score  = 1
    else:
        resultw.config(text="I chose "   cmove   ", you lose!")
        score -= 1

    scorel.config(text="Your score is: "   str(score))

root = Tk()

scorel = Label(root, text="Your score is: "   str(score))
scorel.grid(row=0, column=1)

resultw = Label(root, text="")
resultw.grid(row=2, column=1)

Button(root, text="Rock", command=rock).grid(row=3, column=0, ipadx=30, ipady=20)
Button(root, text="Paper", command=paper).grid(row=3, column=1, ipadx=30, ipady=20)
Button(root, text="Scissors", command=scissors).grid(row=3, column=2, ipadx=30, ipady=20)

root.mainloop()
  • Related