【问题标题】:Labels appearing buffered, but Python code is running unbuffered标签出现缓冲,但 Python 代码正在无缓冲运行
【发布时间】:2020-04-04 10:49:07
【问题描述】:

我目前正在使用 Python3 在 Sublime Text 上创建二十一点游戏。庄家牌一张一张抽出来的时候,你可以在控制台看到一张一张抽出来,用after()调用不同的函数,但在实际的GUI中,所有的牌都是同时出现的,就在结束。

下面是部分代码:

def d_pick_a_card():
    global d_hand
    global card
    global deck
    global d_counter
    global cvpath
    global cardvalue 
    global d_card_relx
    global cv 
    cardvalue = 0
    card = deck.pop(0)
    print('Drew',card)
    if card == 'J':
        cardvalue = 10
        cvpath = 11
    elif card == 'Q':
        cardvalue = 10
        cvpath = 12
    elif card == 'K':
        cardvalue = 10
        cvpath = 13
    elif card == 'A':
        cardvalue = 11
        cvpath = 14
    else:
        cardvalue = card
        cvpath = card
    d_hand.append(cardvalue)
    random.shuffle(deck)
    random.shuffle(deck)
    d_counter += 1

#THIS IS THE FUNCTION THAT SHOWS THE VISUAL CARD ON THE GUI
def d_pick_vis(cardvalue, d_counter):
    global dealers_turn
    global card_frame
    card_frame = tk.Label(top, image = cv[cvpath]) 
    card_frame.place(relx= d_card_relx[d_counter], rely=.18, anchor='center')
    widgetList.append(card_frame)

def dturnfunc():
    global dealersturn_img
    holding_img1.pack_forget()
    dealersturn_img = tk.Label(top, image = dealersturn)
    dealersturn_img.img = dealersturnpath
    dealersturn_img.pack()
    top.after(500, dealers_turn())

def dealers_turn():
    global cardvalue
    global d_counter
    d_pick_a_card()
    d_pick_vis(cardvalue, d_counter)
    print("The Dealers hand is:", sum(d_hand))
    print('\n')
    if sum(d_hand) <= sum(hand):
        print('Dealer picks again')
        top.after(500,dealers_turn_2())
    elif sum(d_hand) == 21:
        print('Dealer Wins')
    elif sum(d_hand) >= 22:
        print('You win!')
    else:
        print('Dealer Wins')

【问题讨论】:

  • 虽然这可能无法解决您的问题。您应该使用top.after(500, dealers_turn) 而不是top.after(500, dealers_turn())

标签: python user-interface tkinter buffered


【解决方案1】:

您正在调用after() 中的函数,而是这样做:

top.after(500, dealers_turn)  # Note: just pass in the name of the function

...

top.after(500, dealers_turn_2)

您需要传入对该函数的引用,after() 将在时间过去后call 该函数。

当您自己调用函数时,您传入返回值,这可能是None,我猜after() 会忽略它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-22
    • 2014-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多