【发布时间】:2021-04-29 17:36:04
【问题描述】:
我已经承担了使用 python 和 tkinter 制作魔方计时器的任务。我想知道是否有一种方法可以使用“输入”键或空格键来启动/停止秒表。我已经能够在不使用 tkinter 窗口的情况下实现这一点,但我无法弄清楚如何让 tkinter 检测按键而无需更改输入字段。我附上的代码是没有 tkinter 的秒表功能。
def stopwatch():
import time
print("")
start = input("Press 'Enter' to start timer")
begin = time.time()
endtimer = input("Press 'Enter' to end the timer")
end = time.time()
time = end-begin
time = float(round(time,3))
text = ""
if time >= 60.999:
newTime = time/60
text += ("The final time was " + str(newTime) + " minutes")
else:
text += ("The final time was " + str(time) + " seconds")
return text
使用 TKINTER 的文件
from tkinter import *
from timer import *
from scrambler import *
from colors import *
window = Tk()
window.title("Bryson's Scrambler")
lbl = Label(window, text=sprint(valid(s)))
lbl.grid(column=0, row=0)
lbl2 = Label(window, text=stopwatch())
lbl2.grid(column=10, row=10)
window.mainloop()
【问题讨论】:
-
您需要将您的代码放在使用 tkinter 的问题中,您在更改输入字段时遇到问题 - 不清楚这是否必要,但如果没有看到代码,我们无法判断。
-
@martineau 我刚刚做到了。谢谢
标签: python tkinter input stopwatch new-window