基于python写个国庆假期倒计时程序
作者:易小侠 发布时间:2022-06-04 20:35:32
标签:python,假期,倒计时
国庆假期快到了,想查查还有几天几小时到假期,这对程序员小菜一碟,轻轻松松用python写个倒计时程序(天、时、分、秒),助你熬到假期!
一、先看效果:
二、安装python:
1、下载安装python
下载安装python3.9.6,进入python官方网站:http://www.python.org/
点击Python 3.9.6
直接安装即可。
2、验证安装成功。
按win+R输入cmd,打开控制台,输入python -V,输出python版本号说明安装成功。
三、代码
##import library
from tkinter import *
import time
from datetime import datetime,timedelta
################GUI to display window ##########################
root = Tk()
root.geometry('450x300')
root.resizable(0,0)
root.config(bg ='blanched almond')
root.title('国庆倒计时')
Label(root, text = '国庆倒计时' , font = 'arial 20 bold', bg ='papaya whip').pack()
############GUI to display current time#######################
Label(root, font ='arial 15 bold', text = ' 当前时间:', bg = 'papaya whip').place(x = 40 ,y = 70)
#######################GUI to set the future time ##########
Label(root, font ='arial 15 bold', text = ' 到达时间:', bg = 'papaya whip').place(x = 40 ,y = 110)
#set year
year_set = StringVar()
Entry(root, textvariable =year_set , width = 4, font = 'arial 12').place(x=175, y=115)
Label(root, font ='arial 15', text = '-', bg = 'papaya whip').place(x = 215 ,y = 110)
year_set.set('0000')
#set month
month_set= StringVar()
Entry(root, textvariable =month_set, width =2, font = 'arial 12').place(x=235, y=115)
Label(root, font ='arial 15', text ='-', bg = 'papaya whip').place(x = 260 ,y = 110)
month_set.set('00')
#set day
day_set= StringVar()
Entry(root, textvariable =day_set, width =2, font = 'arial 12').place(x=275, y=115)
day_set.set('00')
# set hour
hour_set= StringVar()
Entry(root, textvariable =hour_set, width =2, font = 'arial 12').place(x=305, y=115)
Label(root, font ='arial 15', text = ':', bg = 'papaya whip').place(x = 330 ,y = 110)
hour_set.set('00')
# set min
min_set= StringVar()
Entry(root, textvariable =min_set, width =2, font = 'arial 12').place(x=345, y=115)
Label(root, font ='arial 15', text = ':', bg = 'papaya whip').place(x = 370 ,y = 110)
min_set.set('00')
# set sec
sec_set= StringVar()
Entry(root, textvariable =sec_set, width =2, font = 'arial 12').place(x=385, y=115)
sec_set.set('00')
#######################GUI to display timer countdown ##########
Label(root, font ='arial 15 bold', text = ' 倒计时:', bg ='papaya whip').place(x = 40 ,y = 150)
#storing seconds
sec = StringVar()
Entry(root, textvariable = sec, width = 2, font = 'arial 12').place(x=325, y=155)
Label(root, font ='arial 15', text = '秒', bg = 'papaya whip').place(x = 350 ,y = 150)
sec.set('00')
#storing minutes
mins= StringVar()
Entry(root, textvariable = mins, width =2, font = 'arial 12').place(x=275, y=155)
Label(root, font ='arial 15', text = '分', bg = 'papaya whip').place(x = 300 ,y = 150)
mins.set('00')
# storing hours
hrs= StringVar()
Entry(root, textvariable = hrs, width =2, font = 'arial 12').place(x=225, y=155)
Label(root, font ='arial 15', text = '时', bg = 'papaya whip').place(x = 250 ,y = 150)
hrs.set('00')
# storing days
days= StringVar()
Entry(root, textvariable = days, width =2, font = 'arial 12').place(x=175, y=155)
Label(root, font ='arial 15', text = '天', bg = 'papaya whip').place(x = 200 ,y = 150)
days.set('00')
#########fun to display current time#############
def clock():
clock_time = time.strftime('%Y-%m-%d %H:%M:%S %p')
curr_time.config(text = clock_time)
curr_time.after(1000,clock)
curr_time =Label(root, font ='arial 15 bold', text = '', fg = 'gray25' ,bg ='papaya whip')
curr_time.place(x = 175 , y = 70)
clock()
##########fun to start countdown########
def countdown():
#now = datetime.now()
#end = datetime((year_set).get(),(month_set).get(),(day_set).get(),(hour_set).get(),(min_set).get(),(sec_set).get(),00);
global seconds_now
now = time.time()
lt_ = time.strptime(f'{(year_set).get()} {(month_set).get()} {(day_set).get()} {(hour_set).get()} {(min_set).get()} {(sec_set).get()}', '%Y %m %d %H %M %S')
end = time.mktime(lt_)
times=int (end-now)
#.total_seconds());
while times > -1:
minute,second = (times // 60 , times % 60)
hour = 0
if minute > 60:
hour , minute = (minute // 60 , minute % 60)
day=0
if hour>24:
day,hour=(hour//24,hour%24)
sec.set(second)
mins.set(minute)
hrs.set(hour)
days.set(day)
root.update()
time.sleep(1)
times -= 1
Button(root, text='START', bd ='5', command = countdown, bg = 'antique white', font = 'arial 10 bold').place(x=150, y=210)
root.mainloop()
四、运行
打开工程文件,在地址栏里输入cmd,按Enter回车,即打开控制台。
输入python main.py,按回车就打开了程序GUI界面。
到达时间填2021年10月1日,按start按钮,就开始放假倒计时啦!
相关资源:基于python的假期倒计时(天、时、分、秒).zip
来源:https://blog.csdn.net/dwf1354046363/article/details/120520840
0
投稿
猜你喜欢
- 本文实例讲述了Golang排列组合算法问题之全排列实现方法。分享给大家供大家参考,具体如下:【排列组合问题】一共N辆火车(0<N<
- 有些朋友看到这个标题可能会有疑问,难道在视图中使用*符号还有何要注意的地方吗?对于这个问题,我们先不必回答,先看一下例子吧。 我这里,使用的
- 本文实例讲述了PHP+redis实现添加处理投票的方法。分享给大家供大家参考,具体如下:<?php header("Cont
- 字典d = {key1 : value1, key2 : value2, key3 : value3 }键必须是唯一的,但值则不必。值可以取
- 如题:我写入关键字到数据库,多的时候用|隔开了,我提取再做相关文章搜索的时候,我怎么提取用|隔开的文字啊,这样我就好用关键字做搜索啊 回复:
- 实现代码/// <summary>/// 去除HTML标记/// </summary&
- 今天在写一个linux下自动备份指定目录下的所有目录的脚本时,遇到了一个问题,由于我是需要备份目录,所以,需要判断扫描的文件是否为目录,当我
- 以下为谷歌国际少年绘画大赛,小学1-3年级作品。看孩子们的logo创意,自愧呀! 彩色世界我们热爱温暖的阳光,“O”代表我们心中的太阳。我们
- CSV文件简单来说CSV文件由任意数目的记录组成,记录间以某种换行符分隔;每条记录由字段组成,字段间的分隔符是其它字符或字符串,最常见的是逗
- 在项目开发中,经常出现这样的需求。在新增或修改一个主表数据时,对应的从表也要进行同步,此时我们是怎么操作的了?典型的方法就是对于主表的各数据
- 前端开发中两个很不错的小技巧, CSS三角形与圆角背景. 的确, 它们都可以通过图片来实现, 但, 抛开用代码实现可以减小图片加载量不说,
- 设置cookie每个cookie都是一个名/值对,可以把下面这样一个字符串赋值给document.cookie:document.cooki
- 例如:JSON字符串:var str1 = '{ "name": "cxh", "
- 在Twinsen Liang的博客上看到一篇名为语义化单单的限定在html么?的文章,文中主要是提及了CSS的命名规则,仔细阅读后,我认为这
- 代理模式的优点代理模式可以保护原对象,控制对原对象的访问;代理模式可以增强原对象的功能,通过代理对象来添加一些额外的功能;代理模式可以提高系
- 在运用xmlhttp组件编写程序中,会碰到 "msxml3.dll 错误 ‘800c0005’&nb
- 如何用Cookie进行登录验证?很简单,看看这两个文件:login.htm请注册登录随风起舞<FORM ACTION=&qu
- 1. Document.form.item 问题 现有问题:  
- 1. 将Oracle 10g client安装包copy到本地才能安装:2. 双击setup 的到:3. 稍后进入安装界面:4. 选择下一步
- 首先,你需要去有道翻译API官网去申请key:http://fanyi.youdao.com/openapi?path=data-mode得