3
我對python很陌生,我正在嘗試爲自己做一個小項目,但我不明白如何在stop函數中使用start函數的初始時間變量,在那裏我可以用它做數學運算。這是我目前的代碼:如何測量按下python中的按鈕與tkinter之間的流逝時間?
import time
import Tkinter
import tkMessageBox
top = Tkinter.Tk()
def start_time():
tkMessageBox.showinfo("Timer", "The timer will now begin")
initial = time.time()
return initial
def stop_time(initial):
final = time.time()
tkMessageBox.showinfo("Timer", final - initial)
Start = Tkinter.Button(top, text ="Start", command = start_time)
Stop = Tkinter.Button(top, text ="Stop", command = stop_time)
Start.pack()
Stop.pack()
top.mainloop()