Python Forum

Full Version: clear event before keybinding
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am creating a simple reaction time task for a piece of psychology research.

I created the program in Pygame and it worked well but Pygame has very poor ability to create text GUI so I have attempted shift to tkinter and I am attempting to rebuild the test.

Essentially the letter 'A' is presented on the screen and the time taken to press key 'A' is measured.
whiteA=canvas.create_text(200,550, text="A"...)
startTime=time.perf_counter()
root.bind("<Key>", lambda event: calc_react_time(event, startTime, whiteA))

Problem is any key press 'A' prior to presentation of the on-screen stimulus 'A' triggers the keybind immediately, giving a close-to-zero reaction time.

Pygame has event clearing syntax that prevents the key already entered in the buffer from triggering the keybind (?clears the buffer)...
pygame.event.clear()
display_surface.blit(textA_white, textRectA)
pygame.display.update()

I have been unable to find anything similar in Tkinter. Am I missing anything? How to clear the buffer immediately prior to presenting the stimulus as in Pygame?

Also pygame has a wait command (waiting for key entry before continuing)
event = pygame.event.wait()

Does Tkinter have a similar function?

Many thanks, Skyerosebud
You could perhaps call root.update_idletasks() to flush the event queue before binding the event.
Didn't work sorry.

root.update_idletasks()
whiteA=canvas.create_text(200,550, text="A", fill = "white", font =("arialblack", 300, "bold"), anchor=W)
startTime=time.perf_counter()
root.bind("<Key>", lambda event: calc_react_time(event, startTime, whiteA))

Any other ideas? Regards Skyerosebud
Can you post a runable program so that we try to reproduce the issue?