由于工作需要,换季剩余时间成为需要。tkinter界面上实时显示时间(每秒),花了自己不少时间摸索,最后实现了。
效果如图,代码后面给出,仅供参考。
实时时间显示
实现代码如下:
# 实时时间及换季维护剩余时长标签:剩余时间,记录,self,dead,tm,实时,time,date,now From: https://www.cnblogs.com/yujiepla/p/16778160.html
def get_date():
import datetime
date_now = time.localtime(time.time())
now_year = date_now[0]
now_month = date_now[1]
now_day = date_now[2]
quarter = int((int(now_month) - 1) / 3) + 1 # print(quarter) 获取当前月份所在季度
# 防止在每季度最后一天时,计算为负数,显示不正确,多取一天
if quarter == 1:
q1_deadline = datetime.date(now_year, 4, 1)
q1_time = str(q1_deadline.strftime('%Y-%m-%d'))
return q1_time
elif quarter == 2:
q2_deadline = datetime.date(now_year, 7, 1)
q2_time = str(q2_deadline.strftime('%Y-%m-%d'))
return q2_time
elif quarter == 3:
q3_deadline = datetime.date(now_year, 10, 1)
q3_time = str(q3_deadline.strftime('%Y-%m-%d'))
return q3_time
elif quarter == 4:
q4_deadline = datetime.date(now_year+1, 1, 1) #第二年的1月1日00:00
q4_time = str(q4_deadline.strftime('%Y-%m-%d'))
return q4_time
#如2022年第四季度,则返回2023-01-01
ticks = time.localtime(time.time())
# print(ticks) 获取当前时间time.struct_time(tm_year=2022, tm_mon=1, tm_mday=1, tm_hour=1, tm_min=1, tm_sec=1, tm_wday=1, tm_yday=1, tm_isdst=0)
now_year = ticks[0]
now_month = ticks[1]
now_quarter = int((int(now_month) - 1) / 3) + 1
# print("现在是第%s年" % now_year,"现在是第%s月" % now_month,ticks[2], "现在是第%s季度" % now_quarter)
deadline = get_date()
# print(deadline)
self.label_time2 = tk.Label(self.root, text=deadline, bg="#005bac", fg="#ECF5FF",
font=("方正粗黑宋", 16))
def deadline():
s = get_date()
# print(s,type(s)) #<class 'str'>
now_time = time.time()
now_time_s = str(time.strftime('%Y-%m-%d'))
# print(now_time,now_time_s,time.localtime(now_time)) #time.struct_time(tm_year=2022, tm_mon=9, tm_mday=30, tm_hour=11, tm_min=48, tm_sec=46, tm_wday=4, tm_yday=273, tm_isdst=0)
aid_date = datetime.datetime.strptime(s, "%Y-%m-%d")
#print(aid_date) #2022三季度2022-09-30 00:00:00
aid_time = int(time.mktime(aid_date.timetuple())) # 转化为时间戳
dead_line = int(aid_time - now_time) # 时间差
dead_month = dead_line // (60 * 60 * 24 * 30)
# print(dead_month,type(dead_month))
dead_days = dead_line // (60 * 60 * 24) % 30
# print(dead_days)
dead_hours = dead_line // (60 * 60) % 24 % 30
dead_minutes = dead_line // 60 % 60
dead_seconds = dead_line % 60
content = '换季维护(余):%s月%s天%02d时%02d分%02d秒' % (dead_month, dead_days, dead_hours, dead_minutes, dead_seconds)
# print(content,type(content)) <class 'str'>
return content
def Refresh():
try:
content = deadline() ##获取倒计时时间
self.label_time2["text"] = content ##更新label内容
self.root.after(1000, Refresh) # 1秒刷新
except ValueError as e:
tk.messagebox.showerror('出错', '错误代码:%s' % e)
def right_now():
timestring = time.strftime('%Y年%m月%d日 %H:%M:%S', time.localtime())
timestring = "现在北京时间: %s" % timestring
return timestring
def time_run():
timestring = right_now()
self.label_time1["text"] = timestring
self.root.after(1000, time_run)
timestring = right_now()
dead_time = Refresh()
self.label_time1 = tk.Label(self.root, text=timestring, bg="#005bac", fg="#c5c5c5",
font=("方正粗黑宋", 16))
self.label_time2 = tk.Label(self.root, text=dead_time, bg="#005bac", fg="#c5c5c5",
font=("方正粗黑宋", 16)) # bg="#005bac", fg="#ECF5FF",
self.label_time3 = tk.Label(self.root, text="当前第 % s 季度" % now_quarter, bg="#005bac", fg="#c5c5c5",
font=("方正粗黑宋", 16))
self.root.after(1000, time_run) # 界面更新需要加上,时间更新
# 显示登陆用户
try:
self.userLabel1 = tk.Label(self.root, text="当前登录用户:", bg="#003D79", fg="#005bac",
font=("方正琥珀简体", 12, "bold"))
self.userLabel2 = tk.Label(self.root, text="%s" % user_name, bg="#003D79", fg="#ECF5FF",
font=("方正琥珀简体", 12, "bold"))
except NameError:
self.userLabel1 = tk.Label(self.root, text="当前登录用户:", bg="#003D79", fg="#005bac",
font=("方正琥珀简体", 12, "bold"))
self.userLabel2 = tk.Label(self.root, text="Administrator", bg="#003D79", fg="#ECF5FF",
font=("方正琥珀简体", 12, "bold"))