Hi everyone. I would like to ask for help with a rock paper scissors game on tkinter. I have done the codes for a normal game with score counters and all but i would like to set a condition where if win or lose == 5, the buttons get disabled signifying the end of the game. I am at my wits end after days of trial and error. PLEASE HELP!!!
What things did you try?
What happened when you tried them, and how is that different from what is supposed to happen?
This little demo should show you the way.
from tkinter import Tk
from tkinter import ttk
def like():
global likes
likes += 1
if likes > 4:
l_button.state(['disabled'])
r_button.state(['!disabled'])
def reset():
global likes
likes = 0
l_button.state(['!disabled'])
r_button.state(['disabled'])
likes = 0
window = Tk()
window.geometry("150x100")
window.title("Test")
l_button = ttk.Button(window)
r_button = ttk.Button(window)
window.config(
bg='black'
)
l_button.config(
text="Like!",
command=like,
state='normal',
)
r_button.config(
text="Reset",
command=reset,
state='disabled'
)
l_button.pack()
r_button.pack()
window.mainloop()
Hey Rob, thank you so much for the help! I managed to get it done. Not sure why but it only works if the trace is in between the game lines and TK lines. If you know about it, could you share it with me? Anyway i would like to share with you my results so here it is!
# RPS GUI
# WAS by Praveen Kathirvasan 9C
# Whoever ^ guy is
# Now refurbished By TIE
from tkinter import *
import random
import tkinter
user = int
computer = int
win = 0
lose = 0
# Here, we set how the game works.
def rps(win, lose, user):
computer = random.randrange(1,4)
if user == computer:
var.set("It's a TIE.\n Nobody scored :(")
elif user == 1 and computer == 3:
var.set("You chose Rock, Comp chose Scissors.\nYou Win")
wins.set(wins.get() + 1)
win += 1
elif user == 1 and computer == 2:
var.set("You chose Rock, Comp chose Paper.\nYou Lose")
loss.set(loss.get() + 1)
lose += 1
elif user == 2 and computer == 1:
var.set("You chose Paper, Comp chose Rock.\nYou Win")
wins.set(wins.get() + 1)
win += 1
elif user == 2 and computer == 3:
var.set("You chose Paper, Comp chose Scissors.\nYou Lose")
loss.set(loss.get() + 1)
lose += 1
elif user == 3 and computer == 1:
var.set("You chose Scissors, Comp chose Rock.\nYou Lose")
loss.set(loss.get() + 1)
lose += 1
elif user == 3 and computer == 2:
var.set("You chose Scissors, Comp chose Paper.\nYou Win")
wins.set(wins.get() + 1)
win += 1
# Here, we set the conditions to meet so that the game will end.
def trace_var1(*args):
if wins.get() == 5:
outcome.set("OMG YOU WON!\nYou have won 5 times! Nice!\n")
B1.configure(state=DISABLED)
B2.configure(state=DISABLED)
B3.configure(state=DISABLED)
else:
B1.configure(state=NORMAL)
B2.configure(state=NORMAL)
B3.configure(state=NORMAL)
def trace_var2(*args):
if loss.get() == 5:
outcome.set("YOU LOSER!\nYou have lost 5 times! Disappointing :/\n")
B1.configure(state=DISABLED)
B2.configure(state=DISABLED)
B3.configure(state=DISABLED)
else:
B1.configure(state=NORMAL)
B2.configure(state=NORMAL)
B3.configure(state=NORMAL)
# Window title and size.
top = tkinter.Tk()
top.wm_title("TIE's RPS Game")
top.minsize(width = 300, height = 200)
# Buttons and their assignments.
B1 = tkinter.Button(top, text = "Rock", command = lambda: rps(win, lose, 1))
B1.grid(row = 0, column = 1)
B2 = tkinter.Button(top, text = "Paper", command = lambda: rps(win, lose, 2))
B2.grid(row = 0, column = 2)
B3 = tkinter.Button(top, text = "Scissors", command = lambda: rps(win, lose, 3))
B3.grid(row = 0, column = 3)
# Empty line after the buttons.
space = tkinter.Label(top, text = "")
space.grid(row = 1)
# Initial instructions for the game.
var = StringVar()
var.set("Please click any button above!\n")
intro = Label(top, textvariable = var)
intro.grid(row = 2, column = 2)
# "Wins" in the window with a counter below it.
wins = IntVar()
wins.trace('w',trace_var1)
wins.set(0)
w = Label(top, textvariable = wins)
w.grid(row = 4, column = 1)
labeledW = Label(top, text = "Wins:")
labeledW.grid(row = 3, column = 1)
# "Losses" in the window with a counter below it.
loss = IntVar()
loss.trace('w',trace_var2)
loss.set(0)
l = Label(top, textvariable = loss)
l.grid(row = 4, column = 3)
labeledL = Label(top, text = "Losses:")
labeledL.grid(row = 3,column = 3)
# Rule of the game.
outcome = StringVar()
outcome.set("Rock, Paper, Scissors. First to 5 Wins!\n\n")
ending = Label(top, textvariable = outcome)
ending.grid(row = 5, column = 2)
# Team credits
copy = Label(top, text= "RPS GUI on Python Tkinter. By TIE DBBM02")
copy.grid(row = 6, column = 2)
top.mainloop()
Hey Karl, I tried many arrangements, having a while condition or a while and loop. But in the end i managed to do it! Not sure why the position had to be as such but i will share with you my codes! Thanks for replying as well!
# RPS GUI
# WAS by Praveen Kathirvasan 9C
# Whoever ^ guy is
# Now refurbished By TIE
from tkinter import *
import random
import tkinter
user = int
computer = int
win = 0
lose = 0
# Here, we set how the game works.
def rps(win, lose, user):
computer = random.randrange(1,4)
if user == computer:
var.set("It's a TIE.\n Nobody scored :(")
elif user == 1 and computer == 3:
var.set("You chose Rock, Comp chose Scissors.\nYou Win")
wins.set(wins.get() + 1)
win += 1
elif user == 1 and computer == 2:
var.set("You chose Rock, Comp chose Paper.\nYou Lose")
loss.set(loss.get() + 1)
lose += 1
elif user == 2 and computer == 1:
var.set("You chose Paper, Comp chose Rock.\nYou Win")
wins.set(wins.get() + 1)
win += 1
elif user == 2 and computer == 3:
var.set("You chose Paper, Comp chose Scissors.\nYou Lose")
loss.set(loss.get() + 1)
lose += 1
elif user == 3 and computer == 1:
var.set("You chose Scissors, Comp chose Rock.\nYou Lose")
loss.set(loss.get() + 1)
lose += 1
elif user == 3 and computer == 2:
var.set("You chose Scissors, Comp chose Paper.\nYou Win")
wins.set(wins.get() + 1)
win += 1
# Here, we set the conditions to meet so that the game will end.
def trace_var1(*args):
if wins.get() == 5:
outcome.set("OMG YOU WON!\nYou have won 5 times! Nice!\n")
B1.configure(state=DISABLED)
B2.configure(state=DISABLED)
B3.configure(state=DISABLED)
else:
B1.configure(state=NORMAL)
B2.configure(state=NORMAL)
B3.configure(state=NORMAL)
def trace_var2(*args):
if loss.get() == 5:
outcome.set("YOU LOSER!\nYou have lost 5 times! Disappointing :/\n")
B1.configure(state=DISABLED)
B2.configure(state=DISABLED)
B3.configure(state=DISABLED)
else:
B1.configure(state=NORMAL)
B2.configure(state=NORMAL)
B3.configure(state=NORMAL)
# Window title and size.
top = tkinter.Tk()
top.wm_title("TIE's RPS Game")
top.minsize(width = 300, height = 200)
# Buttons and their assignments.
B1 = tkinter.Button(top, text = "Rock", command = lambda: rps(win, lose, 1))
B1.grid(row = 0, column = 1)
B2 = tkinter.Button(top, text = "Paper", command = lambda: rps(win, lose, 2))
B2.grid(row = 0, column = 2)
B3 = tkinter.Button(top, text = "Scissors", command = lambda: rps(win, lose, 3))
B3.grid(row = 0, column = 3)
# Empty line after the buttons.
space = tkinter.Label(top, text = "")
space.grid(row = 1)
# Initial instructions for the game.
var = StringVar()
var.set("Please click any button above!\n")
intro = Label(top, textvariable = var)
intro.grid(row = 2, column = 2)
# "Wins" in the window with a counter below it.
wins = IntVar()
wins.trace('w',trace_var1)
wins.set(0)
w = Label(top, textvariable = wins)
w.grid(row = 4, column = 1)
labeledW = Label(top, text = "Wins:")
labeledW.grid(row = 3, column = 1)
# "Losses" in the window with a counter below it.
loss = IntVar()
loss.trace('w',trace_var2)
loss.set(0)
l = Label(top, textvariable = loss)
l.grid(row = 4, column = 3)
labeledL = Label(top, text = "Losses:")
labeledL.grid(row = 3,column = 3)
# Rule of the game.
outcome = StringVar()
outcome.set("Rock, Paper, Scissors. First to 5 Wins!\n\n")
ending = Label(top, textvariable = outcome)
ending.grid(row = 5, column = 2)
# Team credits
copy = Label(top, text= "RPS GUI on Python Tkinter. By TIE DBBM02")
copy.grid(row = 6, column = 2)
top.mainloop()