如何让它显示在python中特定时间之前的秒数

时间:2017-03-20 00:32:29

标签: python canvas time tkinter tkinter-canvas

我试图制作一个程序,显示一定时间之前有多少秒(例如凌晨1:30)。使用我当前的代码我怎么能做到这一点。

from tkinter import *
import time

root = Tk()
root.geometry('200x200')
root.title('Timer')
root.state('zoomed')

sec = 60

def tick():
    global sec
    sec += -1
    time['text'] = sec
    time.after(1000, tick)

time = Label(root, fg='cyan')
time.pack()
btn = Button(root, fg='blue', text='Start', command=tick)
btn.pack()
btn.place(relx=0.4, rely=0.60, anchor=CENTER)
btn2 = Button(root, text='Exit', command=root.destroy)
btn2.pack()
btn2.place(relx=0.65, rely=0.60, anchor=CENTER)


root.mainloop()

提前感谢您提供任何帮助

1 个答案:

答案 0 :(得分:2)

假设你有两次:

>>> aa = datetime.datetime(2017,4,3,15,0)
>>> a = datetime.datetime(2017,4,3,12,0)
>>> s = aa -a
>>> s
datetime.timedelta(0, 10800)
>>> s.seconds
10800

您可以通过这种方式获取当前日期时间:

>>> d = datetime.datetime.now()
>>> d
datetime.datetime(2017, 3, 20, 9, 32, 58, 924135)