So I'm trying to create a program in a GUI window that tells the user to press the button
and displays the amount of time the button has been pressed. Here's what the window looks like:
The problem is that clicking the button is not affecting the number of presses, thus it always remains at 0.
Here's my code so far:
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()
I don't understand why it's ignoring the presses = presses + 1
part in updatebuttonPress()
, what exactly am I doing wrong?
You aren't ever calling your updateDisplay function to set the label to have the new value of the presses variable. Just call updateDisplay within your updateButtonPress function.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.