As a part of my self learning projects, I decided to create an application in Python that listens to certain keyevents
and finds the amount of time it takes to type a word. It's been more than 12 hours of straight debugging and loop/logic experiments and all I could do is this:
import Tkinter
import time
KeyWatch = Tkinter.Tk()
WordBoxLegend = Tkinter.Label(KeyWatch, text="Type the required word")
WordBoxLegend.pack(side=Tkinter.LEFT)
WordBox = Tkinter.Entry(KeyWatch)
WordBox.pack(side=Tkinter.LEFT)
TextBoxLegend = Tkinter.Label(KeyWatch, text="Type the same to calculate the time")
TextBoxLegend.pack(side=Tkinter.LEFT)
TextBox = Tkinter.Entry(KeyWatch)
TextBox.pack(side=Tkinter.RIGHT)
WordBox.focus()
def pressed(keyevent):
WordRequire = WordBox.get()
LetterList = tuple(WordRequire)
start = time.time()
LastLetter = str(LetterList[-1])
print len(LetterList())
print LetterList[len(LetterList)]
if keyevent.char in LetterList:
for x in range(0, len(LetterList)):
if LetterList[x] != LastLetter:
print LetterList[x]
TextBox.unbind(str(LetterList[x]))
TextBox.bind(str(LetterList[x+1]))
elif str(LetterList[x]) == LastLetter and x == len(LetterList):
stop = time.time()
totaltime = stop - start
print LetterList[x]
print totaltime
break
TextBox.unbind(LetterList[x])
else:
TextBox.unbind(str(LetterList))
else:
print "Type only the letters from" +str(LetterList)
KeyWatch.mainloop()
TextBox.bind("<Key>", pressed)
After I bind keys and pass it to the pressed
method, I am not sure how to unbind that particular key and bind the next value in the tuple.
Expected Result
If I type the word, 'Obliteration' the program should tell me how much time it takes from the keyevent
"O" to keyevent
"n".
What is the Expected Result for events <Key>
in Tkinter?
This was fun!
I rewrote your code with pep8 in mind.
Variable names were adjusted for readability.
There were some logical mistakes in how you define the start time.
Consider that every time a key is pressed,
pressed()
is called. There was also a hidden problem with how the key press was being detected. Your code had:"<KEY>"
Catches the pressing of the key, you want the RELEASE of the key. Otherwise the function is called before the key is released, and the value entered into the repeat box. I changed it to use<KeyRelease>
instead. (feel free to run the code with my debugging to compare"<KEY>"
and"<KeyRelease>"
)Output with Debugging:
This could probably be done without globals, maybe with a recursive call, but I'll leave that for you to solve.