基于python写个国庆假期倒计时程序
作者:易小侠 时间:2022-06-04 20:35:32
国庆假期快到了,想查查还有几天几小时到假期,这对程序员小菜一碟,轻轻松松用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
标签:python,假期,倒计时
0
投稿
猜你喜欢
asp如何用SA-FileUp上传多个HTML文件?
2010-05-18 18:27:00
js function定义函数的几种不错方法
2024-04-16 09:06:34
C#处理MySql多个返回集的方法
2024-01-21 15:30:08
Python尾递归优化实现代码及原理详解
2023-11-08 15:35:28
Python中Selenium上传文件的几种方式
2022-08-13 04:10:40
轻松学习Javascript闭包函数
2024-04-28 09:46:35
Python OpenCV去除字母后面的杂线操作
2023-08-02 15:18:47
Oracle的默认用户密码
2012-07-11 15:29:22
Python Pygame实战之五款童年经典游戏合集
2023-04-15 14:45:14
Python3远程监控程序的实现方法
2021-07-15 15:03:50
Python标准库与第三方库详解
2021-12-16 04:23:03
python自定义异常实例详解
2022-09-04 23:33:19
MySQL使用binlog日志做数据恢复的实现
2024-01-27 09:17:47
Mysql升级到5.7后遇到的group by查询问题解决
2024-01-16 12:17:50
Golang利用channel协调协程的方法详解
2024-05-08 10:21:54
自定义Django Form中choicefield下拉菜单选取数据库内容实例
2024-01-25 09:02:02
用ASP实现在线压缩与解压缩
2007-09-29 12:13:00
Flask框架单例模式实现方法详解
2023-01-24 17:04:55
Python判断字符串是否包含特定子字符串的多种方法(7种方法)
2021-09-20 02:22:51
Python3标准库之dbm UNIX键-值数据库问题
2024-01-26 15:56:11