作为我自学项目的一部分,我决定用Python创建一个监听某个keyevents
的应用程序,并找到键入单词所需的时间。这是超过12个小时的直接调试和循环/逻辑实验,我所能做的就是:
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)
绑定密钥并将其传递给pressed
方法后,我不知道如何取消绑定该特定密钥并绑定元组中的下一个值。
预期结果
如果我输入“Obliteration”这个词,程序应该告诉我从keyevent
“O”到keyevent
“n”需要多长时间。
Tkinter中事件<Key>
的预期结果是什么?
答案 0 :(得分:4)
这很有趣!
我用pep8重写了你的代码。
调整变量名称的可读性。
如何定义开始时间存在一些逻辑错误。
请注意,每次按下某个键时,都会调用pressed()
。如何检测按键是一个隐藏的问题。你的代码有:
word_repeat_box.bind("<Key>", pressed)
"<KEY>"
抓住按键,你想要钥匙的RELEASE。
否则,在释放键之前调用该函数,并将值输入到重复框中。我将其更改为使用<KeyRelease>
代替。 (随意使用我的调试来运行代码以比较"<KEY>"
和"<KeyRelease>"
)
import Tkinter
import time
start = None
stop = None
key_watcher = Tkinter.Tk()
word_entry_label = Tkinter.Label(key_watcher, text="Type the required word")
word_entry_label.pack(side=Tkinter.LEFT)
word_entry_box = Tkinter.Entry(key_watcher)
word_entry_box.pack(side=Tkinter.LEFT)
word_repeat_label = Tkinter.Label(key_watcher,
text="Type the same to calculate the time")
word_repeat_label.pack(side=Tkinter.LEFT)
word_repeat_box = Tkinter.Entry(key_watcher)
word_repeat_box.pack(side=Tkinter.RIGHT)
word_entry_box.focus()
def pressed(keyevent):
# print keyevent #Left this in for debugging, you can see the key press.
# start = time.time() #start time will be reset every time this is called.
first_word = word_entry_box.get()
first_word_size = len(first_word)
first_letter_list = tuple(first_word)
first_word_first_letter = str(first_letter_list[0])
first_word_last_letter = str(first_letter_list[-1])
repeat_word = word_repeat_box.get()
repeat_word_size = len(repeat_word)
if repeat_word_size > 0:
repeat_letter_list = tuple(repeat_word)
repeat_word_first_letter = str(repeat_letter_list[0])
repeat_word_last_letter = str(repeat_letter_list[-1])
# Lets see whats happening on each press:
print "Pressed:", keyevent.char
print "First word:", first_word, "size", first_word_size
print "repeat word:", repeat_word, "size", repeat_word_size
# now that all the things we want to look at are defined, lets logic:
if repeat_word_size == 1:
global start
start = time.time()
print "time started!", start
if first_word_size == repeat_word_size:
print "words are same size"
if first_word_last_letter == repeat_word_last_letter:
global end
stop = time.time()
totaltime = stop - start
print "Total time taken", totaltime
word_repeat_box.bind("<KeyRelease>", pressed)
key_watcher.mainloop() # mainloop() should be called last, it starts your loop
使用调试输出:
Pressed: t
First word: test size 4
repeat word: t size 1
time started! 1434046015.13
Pressed: e
First word: test size 4
repeat word: te size 2
Pressed: s
First word: test size 4
repeat word: tes size 3
Pressed: t
First word: test size 4
repeat word: test size 4
words are same size
Total time taken 1.69620299339
这可能在没有全局变量的情况下完成,也许可以通过递归调用完成,但是我会留给你解决。