Rock Paper Scissors GUI In Python: The GUI code

PHOTO EMBED

Wed Jul 19 2023 14:45:44 GMT+0000 (Coordinated Universal Time)

Saved by @pythonHub #python #python-hub #day3

# FILE: rock_paper_scissors_gui.py
from customtkinter import *
from PIL import Image
from rock_paper_scissors import game


class RPS(CTk):
    """
    A class representing the Rock Paper Scissors game GUI.

    Inherits from customtkinter.CTk.

    Attributes:
    None

    Methods:
    __init__: Initializes the GUI and sets up the game window.
    check: Function to handle the player's choice and display the game result.

    Usage:
    app = RPS()
    app.mainloop()
    """
    def __init__(self) -> None:
        """
        Initializes the Rock Paper Scissors GUI.

        Creates and configures the main game window.

        Parameters:
        None

        Returns:
        None
        """
        super().__init__()
        self.title("Rock Paper Scissors")
        self.geometry("500x600")

        self.Label = CTkLabel(self, text="Choose Rock Paper or Scissors").grid(row=0,column=2,padx=150)

        # Create an instance of the CTkImage class for the 'rock' choice image
        # using the light_image parameter to load the image file from the given path.
        # The size parameter is set to (100, 100) to resize the image to the specified dimensions.
        self.rock_img = CTkImage(light_image=Image.open("rock.png"), size=(100,100))
        # Create an instance of the CTkButton class for the 'rock' choice button.
        # The image parameter is set to the previously created CTkImage instance,
        # so the button will display the 'rock' image as its icon.
        # The text parameter is set to an empty string to hide any text on the button.
        # The fg_color parameter is set to "#242424", which represents the foreground color (text color) of the button.
        # The hover_color parameter is set to "#242424", which represents the background color of the button when hovered.
        # The command parameter is set to a lambda function that calls the 'self.check()' method with argument 0,
        # representing the 'rock' choice, when the button is clicked.
        self.ilr = CTkButton(self, image=self.rock_img, text="", fg_color="#242424", hover_color="#242424", command=lambda: self.check(0))
        # Position the 'rock' choice button in the grid layout at row 1 and column 2.
        # The padx parameter is set to 150 to add horizontal padding around the button to create spacing.
        self.ilr.grid(row=1,column=2,padx=150)

        self.paper_img = CTkImage(light_image=Image.open("paper.png"), size=(100,100))
        self.ilp = CTkButton(self, image=self.paper_img, text="", fg_color="#242424", hover_color="#242424", command=lambda: self.check(1))
        self.ilp.grid(row=2,column=2,padx=150)

        self.scissors_img = CTkImage(light_image=Image.open("scissors.png"), size=(100,100))
        self.ils = CTkButton(self, image=self.scissors_img, text="",fg_color="#242424", hover_color="#242424", command=lambda: self.check(2))
        self.ils.grid(row=3,column=2,padx=150)


        self.qb = CTkButton(self, text="Exit", fg_color="red", hover_color="pink", text_color="white", command=self.quit)
        self.qb.grid(row=4,column=2,padx=150,pady=10)

    def check(self, player_choice: int) -> None:
        """
        Function to handle the player's choice and display the game result.

        Parameters:
        player_choice (int): The player's choice. Should be an integer between 0 and 2,
                             where 0 represents 'rock', 1 represents 'paper', and
                             2 represents 'scissors'.

        Returns:
        None
        """
        result = game(player_choice)
        res1 = CTkLabel(self, text=result[0],text_color ="#ADD8E6").grid(row=5,column=2,padx=150,pady=10)
        res2 = CTkLabel(self, text=result[1], text_color="pink").grid(row=6,column=2,padx=150,pady=10)
        res3 = CTkLabel(self, text=result[2]).grid(row=7,column=2,padx=150,pady=10)


if __name__ == "__main__":
    app = RPS()
    app.mainloop()
content_copyCOPY

This is GUI class base class is linked here

https://www.thiscodeworks.com/rock-paper-scissors-gui-in-python-python-python-hub-day3/64b7f6af5d31ad00134f17c1#!