[Tkinter] clear event before keybinding - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] clear event before keybinding (/thread-18727.html) |
clear event before keybinding - skyerosebud - May-29-2019 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 RE: clear event before keybinding - Gribouillis - May-29-2019 You could perhaps call root.update_idletasks() to flush the event queue before binding the event.
RE: clear event before keybinding - skyerosebud - May-29-2019 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 RE: clear event before keybinding - Gribouillis - May-29-2019 Can you post a runable program so that we try to reproduce the issue? |