基于python写个国庆假期倒计时程序

 更新时间:2021年09月30日 10:58:54   作者:易小侠  
国庆假期快到了,想查查还有几天几小时到假期,这对程序员小菜一碟,轻轻松松用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

到此这篇关于用python写个国庆假期倒计时程序的文章就介绍到这了,更多相关python倒计时内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • python密码学实现文件加密教程

    python密码学实现文件加密教程

    这篇文章主要为大家介绍了python密码学实现文件加密教程,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • python采用django框架实现支付宝即时到帐接口

    python采用django框架实现支付宝即时到帐接口

    这篇文章主要介绍了python采用django框架实现支付宝即时到帐接口的相关资料,需要的朋友可以参考下
    2016-05-05
  • Python中的多线程实例(简单易懂)

    Python中的多线程实例(简单易懂)

    这篇文章主要介绍了Python中的多线程实例,一个CPU,将时间切成一片一片的,CPU轮转着去处理一件一件的事情,到了规定的时间片就处理下一件事情,更多的相关内容需要的小伙伴可以参考下面文章详细
    2022-06-06
  • python实现拉普拉斯特征图降维示例

    python实现拉普拉斯特征图降维示例

    今天小编就为大家分享一篇python实现拉普拉斯特征图降维示例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-11-11
  • Python函数式编程之面向过程面向对象及函数式简析

    Python函数式编程之面向过程面向对象及函数式简析

    这一番我们要学习点有难度的了,因此将降低阅读与理解难度,尽量采用大白话为你铺垫,因为涉及的一些概念也是借鉴的其它编程语言的风格,而且实际落地中存在部分争议不过多学一点,总是没有坏处的
    2021-09-09
  • 解读python cvxpy下SDP问题编程

    解读python cvxpy下SDP问题编程

    这篇文章主要介绍了解读python cvxpy下SDP问题编程,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-12-12
  • python中shape[0]与shape[1]的说明

    python中shape[0]与shape[1]的说明

    这篇文章主要介绍了python中shape[0]与shape[1]的说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08
  • Python 通过requests实现腾讯新闻抓取爬虫的方法

    Python 通过requests实现腾讯新闻抓取爬虫的方法

    今天小编就为大家分享一篇Python 通过requests实现腾讯新闻抓取爬虫的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-02-02
  • python的staticmethod与classmethod实现实例代码

    python的staticmethod与classmethod实现实例代码

    这篇文章主要介绍了python的staticmethod与classmethod实现实例代码,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
    2018-02-02
  • Python语言编写智力问答小游戏功能

    Python语言编写智力问答小游戏功能

    这篇文章主要介绍了使用Python代码语言简单编写一个轻松益智的小游戏,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-10-10

最新评论