0

I Have just written a blackjack game in which I use Tkinter, Every part of the code works fine except in the section where I press the Stand button (Where the dealer_hit function starts) I want the dealer to put down the cards one by one.

I used time.sleep method to make my loop wait a few seconds and repeat every two seconds


import time
def dealer_hit(): # The action when you hit the stand button
    if player_score < 21 and player_score != 0: # If statement to make sure the person is using the button at the right time
        while dealer_score < 17: # loop to make sure the dealer doesn't stop until his score is more than 17
            current_score = dealer_score_update(new_Cards)  # get the next card from the deck
            print('got') # print log
            dealer_result_text.set(current_score)  # Update the label which contains points
            print('set') # print log
            tkinter.Label(dealer_cards_frame, image=next_card_image).pack(side='left')  # Put the image in the specific frame of cards
            print('image') # print log
            time.sleep(2)   # wait 2 seconds and do the loop again
        final_comparison()      # a function to compare the results after the I Have just written a blackjack game in which I use Tkinter, Every part of the code works fine except in the section where I press the Stand button (Where the dealer_hit function starts) I want the dealer to put down the cards one by one.

the sleep method seems to be working fine and the logs print at the right time but the tkinter window seems to get freezed and won't do anything until the function is completely done, I wonder if this has to do anything with the command= parameter in the code.

whole code = https://paste.pythondiscord.com/akodovuqed.py

Mili
  • 1
  • 1
  • Possible duplicate of [Program freezing during the execution of a function in Tkinter](https://stackoverflow.com/questions/10847626/program-freezing-during-the-execution-of-a-function-in-tkinter) – stovfl Sep 19 '19 at 10:54

1 Answers1

0

Your code is blocking. When you're using time.sleep(), the entire script goes to sleep, including the GUI.
You probably have to separate gui/window and script into threads so they can wait on each other.

More about threading here: https://docs.python.org/3.5/library/threading.html

NoSplitSherlock
  • 605
  • 4
  • 19