-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRPS_tk.py
More file actions
45 lines (34 loc) · 1.64 KB
/
Copy pathRPS_tk.py
File metadata and controls
45 lines (34 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#Write a Python program to create an application to play a rock paper scissor game between a user and the computer,
# using the Python Tkinter library.
import tkinter as tk
from tkinter import messagebox
import random
def check_winner(user_choice, computer_choice):
if user_choice == computer_choice:
return "It's a tie!"
elif (user_choice == "Rock" and computer_choice == "Scissors") or \
(user_choice == "Scissors" and computer_choice == "Paper") or \
(user_choice == "Paper" and computer_choice == "Rock"):
return "You win!"
else:
return "Computer wins!"
def user_choice_made(choice):
choices = ["Rock", "Paper", "Scissors"]
computer_choice = random.choice(choices)
result = check_winner(choice, computer_choice)
result_label.config(text=f"Computer chose: {computer_choice}\n{result}")
root = tk.Tk()
root.title("Rock Paper Scissors Game")
title_label = tk.Label(root, text="Rock Paper Scissors Game", font=("Helvetica", 18))
title_label.pack(pady=10)
info_label = tk.Label(root, text="Choose your move:", font=("Helvetica", 14))
info_label.pack(pady=5)
rock_button = tk.Button(root, text="Rock", width=20, height=2, command=lambda: user_choice_made("Rock"))
rock_button.pack(pady=5)
paper_button = tk.Button(root, text="Paper", width=20, height=2, command=lambda: user_choice_made("Paper"))
paper_button.pack(pady=5)
scissors_button = tk.Button(root, text="Scissors", width=20, height=2, command=lambda: user_choice_made("Scissors"))
scissors_button.pack(pady=5)
result_label = tk.Label(root, text="Your result will appear here", font=("Helvetica", 14))
result_label.pack(pady=20)
root.mainloop()