Question: make a python gui wth radio buttons for rock paper scissors game from random import randint #need to be able to return randomly from tkinter
make a python gui wth radio buttons for rock paper scissors game
from random import randint #need to be able to return randomly
from tkinter import *
import tkinter
class TestRPS:
def __init__(self):
self.main_window = tkinter.Tk()
#create frames
self.input_frame = tkinter.Frame(self.main_window)
self.output_frame = tkinter.Frame(self.main_window)
self.button_frame = tkinter.Frame(self.main_window)
#create and pack widgets for input
self.input_label = tkinter.Label(self.input_frame,text ='Rock,Paper,or Scissors?')
# self.input = tkinter.StringVar()
self.input_entry = tkinter.Entry(self.input_frame,width =10)
self.input_label.pack(side='left')
self.input_entry.pack(side='left')
#create and pack widgets for output
# self.output =tkinter.StringVar
self.result_label = tkinter.Label(self.output_frame,
text='Result:')
self.output = tkinter.StringVar() # To update avg_label
self.output_label = tkinter.Label(self.output_frame,
textvariable=self.output)
self.result_label.pack(side='left')
self.output_label.pack(side='left')
#Create and pack the button widgets.
v = tkinter.IntVar()
# self.input_label = tkinter.Label(self.input_frame,text ='Rock,Paper,Or Scissors?')
self.input_entry1 = tkinter.Radiobutton(self.main_window,
text="Rock",
padx = 20,
variable=v,
value=1).pack(anchor=tkinter.W)
self.input_entry2 = tkinter.Radiobutton(self.main_window,
text="Paper",
padx = 20,
variable=v,
value=2).pack(anchor=tkinter.W)
self.input_entry3 = tkinter.Radiobutton(self.main_window,
text="Scissors",
padx = 20,
variable=v,
value=2).pack(anchor=tkinter.W)
self.calc_button = tkinter.Button(self.button_frame,
text='Enter',
command=self.calc_RPS)
self.quit_button = tkinter.Button(self.button_frame,
text='Quit',
command=self.main_window.destroy)
self.calc_button.pack(side='left')
self.quit_button.pack(side='left')
#pack frames
self.input_frame.pack()
self.button_frame.pack()
self.output_frame.pack()
#start the main loop
tkinter.mainloop()
def calc_RPS(self):
self.input = tkinter.StringVar(self.input_entry.get())
valids = ["Rock", "Paper", "Scissors"] # only user choices
PC = valids[randint(0,2)] #playing against computer in a way
# self.inputverify = self.input
you1 = self.input
you = False
while you == False:
if you1 == PC: #if you enter same as computer
print("Tie!")
return
elif you1 == "Rock": #if you pick rock
if PC == "Paper": #and the computer picks paper
print("You lost", PC, "covers", you1) #you lose
else:
print("You won", you1, "smashes", PC) #if you pick anything other than rock you win in this case
you = True
elif you1 == "Paper": #if you pick paper
if PC == "Scissors": #and pc picks scissors
print("You lost", PC, "cut", you1) #you loses
else:
print("You won", you1, "covers", PC) #if you pick anything other than paper you win
you = True
elif you1 == "Scissors":#if you pick scissors
if PC == "Rock": #and computer picks rocks
print("You lost", PC, "smashes", you1) #you lose
else:
print("You won", you1, "cut", PC) #if you pick anything other thn scissors you win
you = True
else: #cannot enter anything other than Rock,Paper or Scissors case sensitive
print(" Invalid Input. Enter only Rock, Paper and Scissors.")
you = False
self.input.set(self.output)
test_input = TestRPS()
window.mainloop()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
