Есть ли способ исправить, пока цикл не обновляет метку tkinter с time.sleep? - PullRequest
0 голосов
/ 19 сентября 2019

Я только что написал игру в блэкджек, в которой я использую Tkinter. Каждая часть кода работает нормально, за исключением раздела, в котором я нажимаю кнопку Stand (где запускается функция dealer_hit). Я хочу, чтобы дилер отложил карты по одному.на единицу.

Я использовал метод time.sleep, чтобы заставить мой цикл подождать несколько секунд и повторять каждые две секунды


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.

метод сна работает нормально, и журналы печатаются св нужное время, но окно tkinter, кажется, зависает и не будет ничего делать, пока функция не будет полностью выполнена, интересно, нужно ли это что-то делать с параметром command= в коде.

весь код= https://paste.pythondiscord.com/akodovuqed.py

1 Ответ

0 голосов
/ 19 сентября 2019

Ваш код блокируется.Когда вы using time.sleep(), сценарий весь переходит в спящий режим, включая GUI .
Возможно, вам придется разделить графический интерфейс / окно и сценарий на потоки, чтобы они могли ждатьдруг на друга.

Подробнее о потоке здесь: https://docs.python.org/3.5/library/threading.html

...