The purpose of my program is to log keypresses in a text editor and display with a timer in real time. Functionality would be similar to manually opening a text editor and typing, which provides immediate feedback.
The code below logs keypresses to the text editor, but output only shows once the program finishes. How can I open the txt file and show keypresses and timer value in real time?
from tkinter import *
import time
import keyboard
root = Tk()
root.title('quick_data')
session_duration = IntVar()
def startSession():
f = open("quick_data.txt", "a")
for remaining in range(1, int(session_duration.get() + 1), +1):
if (keyboard.is_pressed("a")):
r = "{:2d} s".format(remaining) + " --> response 1"
elif (keyboard.is_pressed("b")):
r = "{:2d} s".format(remaining) + " --> response 2"
else:
r = "{:2d} s".format(remaining)
f.write(r)
f.write("\n")
f.flush()
time.sleep(1)
Label(root, text="Session duration").grid(row=1, sticky=W)
Entry(root, textvariable = session_duration).grid(row=1, column=1, sticky=E)
WSignUp = Button(root, text="Start Session", command=startSession).grid(row=3, column=0, sticky=W)
root.mainloop()