I'm new to programming and have been trying to get the hang of things when it comes to functions and am starting to explore Tkinter. I followed this tutorial on rock, paper, scissors and I have a reset button that when the user clicks on it, it'll clear the results and what the user inputted as their choice except it seems like it's not clearing the computer's choice i.e computer selects rock, click reset, computer selects rock again, reset, and so on... so I have to click exit instead and run the program to get a different result.
I tried assigning comp_pick = None
in the def Reset()
and comp_pick.set("")
since I clear the user_pick
like that. I'm just trying to make sense as to what I missed.
#import library
from tkinter import *
import random
#initialize window set up
root = Tk()
root.geometry('500x500')
root.resizable(0,0)
root.title('DataFlair-Rock,Paper,Scissors')
root.config(bg ='pink3')
#heading set up
Label(root, text = 'Rock, Paper ,Scissors' , font='arial 20 bold', bg = 'pink2').pack()
##user choice set up
user_take = StringVar()
Label(root, text = 'choose any one: rock, paper ,scissors' , font='arial 15 bold', bg = 'pink2').place(x = 70,y=70)
Entry(root, font = 'arial 15', textvariable = user_take , bg = 'pink2').place(x=90 , y = 130)
#computer choice using the randint function
comp_pick = random.randint(1,3)
if comp_pick == 1:
comp_pick = 'rock'
elif comp_pick ==2:
comp_pick = 'paper'
else:
comp_pick = 'scissors'
##function to play
Result = StringVar()
def play():
user_pick = user_take.get()
if user_pick == comp_pick:
Result.set('It is a tie. You both have selected ' + user_pick +'.')
elif user_pick == 'rock' and comp_pick == 'paper':
Result.set('You loose! Computer selected paper.')
elif user_pick == 'rock' and comp_pick == 'scissors':
Result.set('You win! Computer selected scissors.')
elif user_pick == 'paper' and comp_pick == 'scissors':
Result.set('You loose! Computer selected scissors.')
elif user_pick == 'paper' and comp_pick == 'rock':
Result.set('You win! Computer selected rock.')
elif user_pick == 'scissors' and comp_pick == 'rock':
Result.set('You loose! Computer selected rock.')
elif user_pick == 'scissors' and comp_pick == 'paper':
Result.set('You win! Computer selected paper.')
else:
Result.set('invalid: choose any one -- rock, paper, scissors')
##fun to reset
def Reset():
Result.set("")
user_take.set("")
comp_pick.set("")
##fun to exit
def Exit():
root.destroy()
###### button
Entry(root, font = 'arial 10 bold', textvariable = Result, bg ='pink2',width = 50,).place(x=25, y = 250)
Button(root, font = 'arial 13 bold', text = 'PLAY' ,padx =5,bg ='pink4' ,command = play).place(x=150,y=190)
Button(root, font = 'arial 13 bold', text = 'RESET' ,padx =5,bg ='pink4' ,command = Reset).place(x=70,y=310)
Button(root, font = 'arial 13 bold', text = 'EXIT' ,padx =5,bg ='pink4' ,command = Exit).place(x=230,y=310)
root.mainloop()