Hey guys, welcome back to the channel. In today’s on-demand video, I’ll guide you through creating a GUI app using the kinter library. The app is a Rock, Paper, Scissors game where you can play against the computer. Let’s first take a look at the app and what needs to be done. Later, I’ll dive into the code explanation.
from tkinter import *
from PIL import ImageTk, Image
import random
# Function to determine the winner
def game(player_choice):
computer_choice = random.choice(options)
# Set player image based on choice
if player_choice == 1:
canvas.itemconfig(player_hand, image=rock_img)
elif player_choice == 2:
canvas.itemconfig(player_hand, image=paper_img)
elif player_choice == 3:
canvas.itemconfig(player_hand, image=scissor_img)
# Set computer image based on choice
canvas.itemconfig(computer_hand, image=options_images[computer_choice])
result = determine_winner(player_choice, computer_choice)
display_result(result)
# Function to determine the winner
def determine_winner(player_choice, computer_choice):
if player_choice == computer_choice:
return "Draw"
elif (player_choice, computer_choice) in [(1, 3), (2, 1), (3, 2)]:
return "You Win!"
else:
return "Computer Wins!"
# Function to display the result
def display_result(result):
canvas.delete("result")
canvas.create_text(150, 150, text=result, font=("Helvetica", 20), tags="result")
# Function to clear the canvas
def clear():
canvas.delete("result")
canvas.itemconfig(player_hand, image=default_img)
canvas.itemconfig(computer_hand, image=default_img)
# Main code
root = Tk()
root.title("Rock Paper Scissor")
root.geometry("400x400")
canvas = Canvas(root, width=300, height=300)
canvas.pack()
options = [1, 2, 3]
options_images = {
1: ImageTk.PhotoImage(Image.open("rock.png")),
2: ImageTk.PhotoImage(Image.open("paper.png")),
3: ImageTk.PhotoImage(Image.open("scissor.png"))
}
default_img = ImageTk.PhotoImage(Image.open("default.png"))
rock_img = ImageTk.PhotoImage(Image.open("rock.png"))
paper_img = ImageTk.PhotoImage(Image.open("paper.png"))
scissor_img = ImageTk.PhotoImage(Image.open("scissor.png"))
# Create player and computer hands
player_hand = canvas.create_image(80, 180, image=default_img, tags="hand")
computer_hand = canvas.create_image(220, 180, image=default_img, tags="hand")
# Labels for player and computer
canvas.create_text(80, 50, text="Player", font=("Arial", 14))
canvas.create_text(220, 50, text="Computer", font=("Arial", 14))
# Buttons for Rock, Paper, Scissor
rock_btn = Button(root, text="Rock", command=lambda: game(1))
rock_btn.pack(side=LEFT)
paper_btn = Button(root, text="Paper", command=lambda: game(2))
paper_btn.pack(side=LEFT)
scissor_btn = Button(root, text="Scissor", command=lambda: game(3))
scissor_btn.pack(side=LEFT)
# Button to clear the result
clear_btn = Button(root, text="Clear", command=clear)
clear_btn.pack()
root.mainloop()
I’ll be using Python for this. Let’s create a new Python file, and I’ll name it ai.py. Now, if you look at the screen, these are the images you’ll need to upload to your app. One, two, and three. Additionally, there’s a button used in the app. The app’s name is “Rock Paper Scissor,” and it includes labels for player and computer names.
Now, moving to the code. First, we need to import the kter library. Since we’ll be using images, I’m importing the PIL library and two modules, ImageTK and Image. Also, I’m using the random module for choosing options randomly in the game.
Let’s start with creating the main window object. root = TK creates a blank GUI app. Then, we set the title and geometry for the window. Now, we create a canvas inside the app to place our elements.
For labels, such as “Player” and “Computer,” and the game options, we use the label widget. These labels need to be placed on the canvas at specific coordinates.
Now, we load the images using the PhotoImage widget. Ensure the paths are correct. We load images for Rock, Paper, and Scissors, and also create transposed versions for flipping the hands.
Moving on, we create a selection image and use the create_image widget to place it on the canvas.
Now, let’s add functionality. I’ve created a function called game with player options stored in a list. The computer randomly chooses an option, and images for player and computer hands are displayed accordingly.
We check for the game results. If it’s a draw, the result is displayed as “Draw.” Otherwise, we use permutations to determine the winner.
There’s also a clear function to remove results from the canvas and reset the images.
Finally, we run the app using root.mainloop(). When you run the script, you’ll see the GUI app with the images and buttons for Rock, Paper, and Scissors.





Leave a Reply