簡體   English   中英

如何獲取程序來更新GUI窗口中的增量? (python 3)

[英]How do I get my program to update the increment in the GUI window? (python 3)

因此,我試圖在GUI窗口中創建一個程序,該程序告訴用戶press the button並顯示press the button的時間。 窗口的外觀如下:

gui窗口

問題在於單擊按鈕不會影響按下次數,因此始終保持為0。

到目前為止,這是我的代碼:

import tkinter

presses = 0

canHitEnter = True

def updateButtonPress():   #????????????????????? 
    global presses
    presses = presses + 1
    if going():
        pressesLabel.after(500, updateButtonPress)

def updateDisplay():
    global presses
    pressesLabel.config(text = 'PRESSES: ' + str(presses))
    empty.after(100, updateDisplay)


def going():
    global presses
    return True


def start(event):
    global canHitEnter
    if canHitEnter == False:
        pass
    else:
        updateButtonPress()
        canHitEnter = False



gui = tkinter.Tk()
gui.title('Press the Button')
gui.geometry('500x400')

startLabel = tkinter.Label(gui, text = 'press enter to start the game', font = 16)
startLabel.pack()

pressesLabel = tkinter.Label(gui, text = 'presses: ' + str(presses), font = 14)
pressesLabel.pack()

buttonLabel = tkinter.Button(gui, text = 'press', command = updateButtonPress)
buttonLabel.pack()

empty = tkinter.Label(gui, text = '_')
empty.pack()


gui.bind('<Return>', start)
gui.mainloop()

我不明白為什么它忽略了updatebuttonPress() presses = presses + 1 updatebuttonPress() presses = presses + 1部分,我到底在做什么錯?

您從未調用過updateDisplay函數來將標簽設置為具有presses變量的新值。 只需在updateButtonPress函數中調用updateDisplay。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM