-1

I'm new at python, was trying to do a Paper Rock Scissors game with tkinter. I did all, but I want to do a 1 sec pause before you can play again. When I use time.sleep(1) it stops program before results of game are shown(code below) however I put time.sleep(1) AFTER it


from tkinter import *
import time
import random

#game variants
game_variants = ("rock", "paper", "scissors")

# Variable for user choise
user_choise = ""

#creating functions for buttons choise
def rock():
    global user_choise
    user_choise = "rock"
    game()

def paper():
    global user_choise
    user_choise = "paper"
    game()

def scissors():
    global user_choise
    user_choise = "scissors"
    game()

# Game rules
def game():
    global bot_choise
    global user_choise

    # bot random choose
    bot_choise = random.choice(game_variants)
    # print(bot_choise)

    if bot_choise == user_choise:
        text_area.config(text="Tie!")
    elif (
            (bot_choise == "rock" and user_choise == "scissors") or
            (bot_choise == "paper" and user_choise == "rock") or
            (bot_choise == "scissors" and user_choise == "paper")
    ):
        text_area.config(text=f"You Lost!\nComputer chose {bot_choise} ")
    else:
        text_area.config(text=f"You won!\nComputer chose {bot_choise}")
    print(user_choise)
    
    time.sleep(1)


# creating a window
root = Tk()
root.geometry("900x400")
root.title("Rock Paper Scissors game")

# a label at the top
text_area = Label(text="Choose a variant below:", font=("consolas",22), height=3, width=55, bg="GREY", highlightthickness=3,
                      highlightbackground="yellow",) # adding an outline (border)
text_area.pack()

# default parameters for buttons
button_height = 10
button_width = 24
font_and_size = "consolas", 15
bg_color = "#2d1a36"
fg_color = "brown"

# creating a second "second" window to use grid, because you can't use a pack and a grind together
frame = Frame(root)
frame.pack()

#making buttons for choice
rock_button = Button(frame, text="Rock",font=font_and_size,fg=fg_color, height=button_height,
                     width=button_width,bg=bg_color,
                     command=rock)
rock_button.grid(row=1,column=0, columnspan=1)
paper_button = Button(frame, text="Paper",font=font_and_size,fg=fg_color,height=button_height,
                      width=button_width,bg=bg_color, command=paper)
paper_button.grid(row=1, column=1)
scissor_button = Button(frame, text="Scissors",font=font_and_size, fg=fg_color, height=button_height,
                        width=button_width,bg=bg_color, command=scissors)
scissor_button.grid(row=1,column=2)

root.mainloop()

The code very simple and with comments. I've tried many "solutions" by position time.sleep in different places, but it still doesnt work how I would like and stops program before it needed

deightt
  • 1
  • 1

1 Answers1

3

Your code is single threaded meaning that if you add a time.sleep(), it will block the excecution of the rest of your code resulting in the whole program pausing. To mitigate this you need to start the game process in a seperate thread to the UI, this will only sleep the code in your game thread and not your UI thread.

Why use threads?

While Creating a GUI there will be a need to do multiple work/operation at backend. Suppose we want to perform 4 operations simultaneously. The problem here is, each operation executes one by one. During execution of one operation the GUI window will also not move and this is why we need threading

How to use threads with tkinker: https://www.geeksforgeeks.org/how-to-use-thread-in-tkinter-python/

1azza
  • 61
  • 3