Python Forum
[Tkinter] clear event before keybinding
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] clear event before keybinding
#1
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
Reply
#2
You could perhaps call root.update_idletasks() to flush the event queue before binding the event.
Reply
#3
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
Reply
#4
Can you post a runable program so that we try to reproduce the issue?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020