如何进行 72 小时倒计时
How can I do a 72 hour countdown
我正在 Tkinter 上做一个计时器,但到目前为止我还没有找到实现倒计时的好方法。我已经尝试使用 datetime 和 time 但不能专门进行 72 小时倒计时。而不是时间下降,它只会根据机器的本地时间增加。你能帮帮我吗?
代码和图像:
import tkinter as tk
import time
countdown = 259200
class Application():
def __init__(self):
self.main = tk.Tk()
self.main.geometry('700x500')
self.main.title('')
self.main.resizable(0,0)
self.CreateWidgets()
self.main.mainloop()
def CreateWidgets(self):
self.now = tk.StringVar()
self.framemain = tk.Frame(self.main)
self.framemainlabel = tk.Frame(self.framemain,bg='black')
self.frametime = tk.Frame(self.framemain,bg='grey')
self.frametiming = tk.Frame(self.frametime,bg='red')
self.timeto = tk.Label(self.frametime,text="TIME LEFT",bg='grey',font=('Trebuchet MS',30),anchor=tk.NW)
self.time = tk.Label(self.frametime,text=self.now,bg='red',font=('Alarm Clock',100))
self.framemain.place(relwidth=1,relheight=1)
self.frametime.place(relwidth=1,relheight=0.8,rely=0.2)
self.framemainlabel.place(relwidth=1,relheight=0.2)
self.frametiming.place()
self.timeto.pack()
self.time.pack()
self.Counting()
def Counting(self):
t=time.strftime('%I:%M:%S',time.localtime())
if t!='':
self.time.config(text=t)
self.main.after(100,self.Counting)
app = Application()
Timer
我实现了一个类似的倒计时 2 分 2 秒。附上以下代码:
class UI(object):
def __init__(self, master, **kwargs):
self.timeLabel = tk.Label(self.master, text="02:00", font=("Times New Roman", 33), wraplength=self.screenwidth)
self.timeRemaining = "02:02"
self.timeLabel.pack()
self.update_timer()
def update_timer(self):
self.after_id = self.master.after(1000, self.update_timer)
self.timeRemaining = str(datetime.datetime.strftime(datetime.datetime.strptime(self.timeRemaining, '%M:%S')-datetime.timedelta(seconds=1), '%M:%S'))
self.timeLabel.config(text=self.timeRemaining)
if self.timeRemaining == "00:00":
# Exit logic here
对于 72 小时倒计时,我认为 strftime
的论点会改变。
我正在 Tkinter 上做一个计时器,但到目前为止我还没有找到实现倒计时的好方法。我已经尝试使用 datetime 和 time 但不能专门进行 72 小时倒计时。而不是时间下降,它只会根据机器的本地时间增加。你能帮帮我吗?
代码和图像:
import tkinter as tk
import time
countdown = 259200
class Application():
def __init__(self):
self.main = tk.Tk()
self.main.geometry('700x500')
self.main.title('')
self.main.resizable(0,0)
self.CreateWidgets()
self.main.mainloop()
def CreateWidgets(self):
self.now = tk.StringVar()
self.framemain = tk.Frame(self.main)
self.framemainlabel = tk.Frame(self.framemain,bg='black')
self.frametime = tk.Frame(self.framemain,bg='grey')
self.frametiming = tk.Frame(self.frametime,bg='red')
self.timeto = tk.Label(self.frametime,text="TIME LEFT",bg='grey',font=('Trebuchet MS',30),anchor=tk.NW)
self.time = tk.Label(self.frametime,text=self.now,bg='red',font=('Alarm Clock',100))
self.framemain.place(relwidth=1,relheight=1)
self.frametime.place(relwidth=1,relheight=0.8,rely=0.2)
self.framemainlabel.place(relwidth=1,relheight=0.2)
self.frametiming.place()
self.timeto.pack()
self.time.pack()
self.Counting()
def Counting(self):
t=time.strftime('%I:%M:%S',time.localtime())
if t!='':
self.time.config(text=t)
self.main.after(100,self.Counting)
app = Application()
Timer
我实现了一个类似的倒计时 2 分 2 秒。附上以下代码:
class UI(object):
def __init__(self, master, **kwargs):
self.timeLabel = tk.Label(self.master, text="02:00", font=("Times New Roman", 33), wraplength=self.screenwidth)
self.timeRemaining = "02:02"
self.timeLabel.pack()
self.update_timer()
def update_timer(self):
self.after_id = self.master.after(1000, self.update_timer)
self.timeRemaining = str(datetime.datetime.strftime(datetime.datetime.strptime(self.timeRemaining, '%M:%S')-datetime.timedelta(seconds=1), '%M:%S'))
self.timeLabel.config(text=self.timeRemaining)
if self.timeRemaining == "00:00":
# Exit logic here
对于 72 小时倒计时,我认为 strftime
的论点会改变。