以下代码从列表中选择一个单词并将其显示在标签上。我还有一个标签,显示用户按下回车键后更新的时间。
import tkinter
import time
import random
score = 0
words = ["Games","Development","Keyboard","Speed","Typer","Anything","Alpha","Zealous","Accurate","Basics","Shortcut","Purpose","Window","Counter","Fortress","Modification","Computer","Science","History","Football","Basketball","Solid","Phantom","Battlefield","Advanced","Warfare","Download","Upload","Antidisestablishmentarianism","Supercalifragilisticexpialidocious","Discombobulation","Liberated","Assassin","Brotherhood","Revelation","Unity","Syndicate","Victory"]
wordcount = 0
start_time = time.time()
def shuffle():
global word, score, start_time
now = time.time()
score = float(start_time - now)
score = round(score,2)
while wordcount < 12:
entry.delete(0, tkinter.END)
word = random.choice(words)
label.config(text=str(word))
scoreLabel.config(text="Time: " + str(score) + "s")
return
def check(event):
global score, wordcount, word, start_time
now = time.time()
score = float(start_time - now)
score = round(score,2)
if entry.get().lower() == word.lower():
scoreLabel.config(text="Time: " + str(score) + "s")
wordcount += 1
wordcounter.config(text="Words: " + str(wordcount))
words.remove(word)
shuffle()
elif entry.get().lower() != word.lower():
scoreLabel.config(text="Time: " + str(score) + "s")
shuffle()
root = tkinter.Tk()
label = tkinter.Label(root, font=('Helvetica', 32))
label.pack()
scoreLabel = tkinter.Label(root, text="Time: " + str(score) + "s", font=('Helvetica', 14))
scoreLabel.pack()
wordcounter = tkinter.Label(root, text="Words: " + str(wordcount), font =("Helvetica", 14))
wordcounter.pack()
go = tkinter.Button(root, text="GO!", command=shuffle)
go.pack()
entry = tkinter.Entry(root)
root.bind("<Return>", check)
entry.pack()
entry.focus_set()
root.mainloop()
我的问题是,无论何时按下回车键,时间都显示为负值,这是错误的。我也尝试过(最后两个没有任何不同):
score = float(now - start_time)
score = float(start_time + now)
score = float(now + start_time)
但仍会产生错误的输出(负值)。
答案 0 :(得分:1)
start_time
的数字始终小于now
。你应该逆转你的减法。
score = float(now - start_time)