有没有办法修复循环不更新tkinter标签的问题时间。睡觉?

2024-04-25 07:50:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚刚写了一个21点游戏,我使用Tkinter,代码的每一部分都很好地工作,除了在我按下站立按钮的部分(庄家点击功能开始的地方),我希望庄家一张一张地放下牌。你知道吗

我曾经时间。睡眠方法使循环等待几秒钟并每两秒钟重复一次


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.

sleep方法似乎工作正常,日志在正确的时间打印,但是tkinter窗口似乎被冻结,在函数完成之前不会做任何事情,我想知道这是否与代码中的command=参数有关。你知道吗

整个代码=https://paste.pythondiscord.com/akodovuqed.py


Tags: theto代码inimagelogtimetkinter
1条回答
网友
1楼 · 发布于 2024-04-25 07:50:16

你的代码被阻塞了。当您using time.sleep()时,整个脚本进入休眠状态,包括GUI
您可能需要将gui/window和脚本分离成线程,以便它们可以互相等待。你知道吗

关于线程的更多信息:https://docs.python.org/3.5/library/threading.html

相关问题 更多 >