基于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


猜你喜欢
- 一、库介绍opencv,face_recognition,numpy,以及dlib注意:安装opencv速度可能过慢,需要更换国内镜像源,参
- 在调用后端接口时,由于后端接口的不规范统一,接口最外层在没有数据时返回的是空数组(其实更想要的是空json对象),而在有数据时返回的是jso
- 支持按照文件夹去批量处理,也可以单独一个文件进行处理,并且可以自定义标识符最近在开发一个答题类的小程序,到了录入试题进行测试的时候了,发现一
- 本文实例为大家分享了pyqt5利用pyqtDesigner实现登录界面的具体代码,供大家参考,具体内容如下为便于操作 界面和逻辑分离逻辑类:
- 之前安装mysql 5.7.12时未做总结,换新电脑,补上安装记录,安装的时候,找了些网友的安装记录,发现好多坑(一)mysql 5.7.1
- 字体的处理在网页设计中无论怎么强调也不为过, 毕竟网页使用来传递信息的, 而最经典最直接的信息传递方式就是文字,&nbs
- 目录简述:实战案例:简述:关于敏感词过滤可以看成是一种文本反垃圾算法,例如 题目:敏感词文本文件 filtered_words.t
- 配置日志在Django中,可以通过logging模块来记录日志。日志记录器是将日志消息传递给日志处理器的对象。当需要记录日志时,可以使用以下
- 本文向大家分享了几段Python生成数字图片的代码,喜欢的朋友可以参考。具体如下:最终版本# -*- coding:utf-8 -*-fro
- 较新的pip已经支持list --outdated了,所以记录一下新的方法:pip list --outdated --format=leg
- eval() 和 exec() 函数都属于 Python 的内置函数,由于这两个函数在功能和用法方面都有相似之处,所以将它们放到一节进行介绍
- 我们有时候会批量处理同一个文件夹下的文件,并且希望读取到一个文件里面便于我们计算操作。比方我有下图一系列的txt文件,我该如何把它们写入一个
- 什么是锁现实生活中的锁是为了保护你的私有物品,在数据库中锁是为了解决资源争抢的问题,锁是数据库系统区别于文件系统的一个关键特性。锁机制用于管
- 简介概念散列算法(Hash Algorithm),又称哈希算法,杂凑算法,是一种从任意文件中创造小的数字「指纹」的方法。与指纹一样,散列算法
- 很多网友在浏览网页时应该会发现很多网页有显示时间和日期的功能,这个不难,使用可视化网页制作软件Drea
- 目录1. 常用的编码2.补充:计算机表示的单位:3.ASCII编码2.GBK和GB2312编码4.Unicode5.UTF-8编码6.编码和
- 思路:先随机排序然后再分组就好了。1、创建表:CREATE TABLE `xdx_test` ( `id` int(11) NOT NULL
- 1. 定义节点// Node 定义节点type Node struct { Data any
- 网页设计遇到最大的麻烦之一莫过于网页对不同浏览器的兼容性问题了,因为IE 6.0 / IE 7.0 / firefox 2 / Opera
- 引用计数在Python源码中,每一个对象都是一个结构体表示,都有一个计数字段。typedef struct_object { i