Skip to content
Advertisement

Entry not updating tkinter python

Im trying to make a rock paper scissors game using the python module tkinter to create a window with an input box. However im struggling to get the entry box to update to another value everytime i press the button.

from tkinter import *
import random

root = Tk()
root.geometry('450x350')
root.configure(bg='yellow')


#Global variables
text = []
buttonClicked = False

def changeValue():
    global buttonClicked
    if buttonClicked:
        buttonClicked=False
    if not buttonClicked:
        buttonClicked=True

def button_command():
    global text
    text.append(player_entry.get())
    player_entry.delete(0, END)

def final_calculation():
    cmpt_choice = random.choice([0, 1, 2])

    def determine_win(user_choice, cmpt_choice):
        return (user_choice + -cmpt_choice) % 3
        
    if text[0] == 'Rock' or text[0] == 'rock':
        player_choice = 0

    elif text[0] == 'Paper' or text[0] == 'paper':
        player_choice = 1

    elif text[0] == 'Scissors' or text[0] == 'scissors':
        player_choice = 2
    
    else:
        player_choice = 3
        print('Something went wrong with the input')

    if player_choice != 3:

        print(determine_win(player_choice, cmpt_choice))
        return determine_win(player_choice, cmpt_choice)


#Player
player_label = Label(root, text='Rock / Paper / Scissors')
player_entry = Entry(root, width= 20)
player_button = Button(root, text="Confirm", command=lambda:[button_command(), changeValue(), final_calculation()])


player_entry.pack()
player_label.pack()
player_button.pack()

#Positioning
player_entry.place(x = 100, y = 175, anchor = 'center')
player_label.place(x = 100, y = 140, anchor = 'center')
player_button.place(x= 205 , y = 175, anchor = 'center')



root.mainloop()

What im struggling with i.e if I was to write Rock as an input (assume computer response is always 0):

Then my output would be 0 this is correct, however if my next input was something random like dsadsad the desired output would be 'Something went wrong with the input'. However it seems like the first input is saved and is continusly used since the output would still be 0.

If we on the other hand start with the input dsdasdasd the output would read: 'Something went wrong with the input'. And if we then wrote the input as rock the output would still read 'Something went wrong with the input' instead of the desired output 0.

Any help appreciated,

OBS! this might not be the most efficient way of programming this form of programm but im new to programming.

Advertisement

Answer

Your if statements are at fault. You are always comparing the user input with the first item on the list, with text[0], you need to change that to text[-1] to get the last item appended to the list. So your function would be:

def final_calculation():
    global player_choice
    cmpt_choice = random.choice([0, 1, 2])

    def determine_win(user_choice, cmpt_choice):
        return (user_choice + -cmpt_choice) % 3
        
    if text[-1].lower() == 'rock':
        player_choice = 0

    elif text[-1].lower() == 'paper':
        player_choice = 1

    elif text[-1].lower() == 'scissors':
        player_choice = 2
    
    else:
        player_choice = 3
        print('Something went wrong with the input')

    if player_choice != 3:

        print(determine_win(player_choice, cmpt_choice))
        return determine_win(player_choice, cmpt_choice) # Button callbacks have no use returning data

You can also remove or and convert all text to lower or upper case and then compare between them. I am also assuming this is not the entire code, because some of your functions don’t have any true purpose (yet?) and returning from a button callback is of no use too.

Advertisement