基于Python实现倒计时工具

 更新时间:2022年08月09日 12:58:17   作者:aguang5241  
这篇文章主要为大家详细介绍了基于Python实现倒计时工具,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

基于Python编写的倒计时工具,供大家参考,具体内容如下

特点:

实时显示当前时间
自动判断用户输入日期,计算当前日期与目标日期相差大概多少年、月、日以及准确的相差天数

运行窗口

运行界面-1

运行界面-2

输入日期-3

结果窗口-4

代码

import time
import tkinter as tk
from tkinter import messagebox

def main():
    window1 = tk.Tk()
    window1.title('计时器【v0.0】')
    window1.geometry('300x200')

    l1 = tk.Label(window1, text = '当前时间:', font = ('宋体', 15))
    l1.place(x = 5, y = 10)
   
    def time_now():
        global seconds_now
        seconds_now = time.time()
        lt = time.localtime(seconds_now)
        time1 = []
        time2 = '%04d年%02d月%02d日    \n    %02d时%02d分%02d秒' % (lt[0], lt[1], lt[2], lt[3], lt[4], lt[5])

        if time2 != time1:
            time1 = time2
            l1_2 = tk.Label(window1, text = time1, font = ('宋体', 20))
            l1_2.configure(text = time2)
            l1_2.place(x = 30, y = 50)
            l1_2.after(200, time_now)
            
    time_now()
    
    def input_time():
        window2 = tk.Tk()
        window2.title('计时器【v0.0】')
        window2.geometry('300x120')

        l2_1 = tk.Label(window2, text = '年', font = ('宋体', 15))
        l2_1.place(x = 90, y = 20)
        l2_2 = tk.Label(window2, text = '月', font = ('宋体', 15))
        l2_2.place(x = 170, y = 20)
        l2_3 = tk.Label(window2, text = '日', font = ('宋体', 15))
        l2_3.place(x = 250, y = 20)
        l2_4 = tk.Label(window2, text = '有效日期【1970/1/2-3001/1/1】', font = ('宋体', 10))
        l2_4.place(x = 50, y = 50)

        year = tk.Entry(window2, text = None, font = ('宋体', 15), width = 5)
        month = tk.Entry(window2, text = None, font = ('宋体', 15), width = 5)
        day = tk.Entry(window2, text = None, font = ('宋体', 15), width = 5)
        year.place(x = 40, y = 20)
        month.place(x = 120, y = 20)
        day.place(x = 200, y = 20)

        def get_time():
            try:
                y = int(year.get())
                m = int(month.get())
                d = int(day.get())
                lt_ = time.strptime(f'{y} {m} {d}', '%Y %m %d')
                seconds_get = time.mktime(lt_)
            except BaseException:
                tk.messagebox.showerror(message='输入有误!')
            else:
                window2.withdraw()    
            
            string1 = '查询日期距离现在还有:'
            string2 = '查询日期距离现在已过去:'

            seconds_lasting = seconds_get - seconds_now
            
            day_lasting = abs(seconds_lasting) // 86400
            month_lasting = 0
            year_lasting = 0
            days = day_lasting
           
            if day_lasting > 356:
                year_lasting = day_lasting // 365
                day_lasting -= year_lasting * 365
                if day_lasting > 30:
                    month_lasting = day_lasting // 30
                    day_lasting -= month_lasting * 30
            elif day_lasting > 30:
                year_lasting = 0
                month_lasting = day_lasting // 30
                day_lasting -= month_lasting * 30 
            else:
                year_lasting, month_lasting = 0, 0
            
            if seconds_lasting > 0:
                prompt = string1
                days += 1
                day_lasting += 1
            else: 
                prompt = string2  
                  
            tk.messagebox.showinfo(message='%s%d天\n大概为%d年%d月%d天' % (prompt, days, year_lasting, month_lasting, day_lasting))   
                
        button2 = tk.Button(window2, text = '开始查询', font = ('宋体', 15), command = get_time)
        button2.place(x = 110, y = 75)
       
        window2.mainloop()

    button1 = tk.Button(window1, text = '输入查询日期', font = ('宋体', 15), command = input_time)
    button1.place(x = 85, y = 125)

    window1.mainloop()

if __name__ == '__main__':
    main()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • python jenkins 打包构建代码的示例代码

    python jenkins 打包构建代码的示例代码

    这篇文章主要介绍了python jenkins 打包构建代码的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-11-11
  • matlab绘制局部放大图图文教程

    matlab绘制局部放大图图文教程

    这篇文章主要给大家介绍了关于matlab绘制局部放大图的相关资料,所谓局部放大即呈现子图,以显示局部细节,需要的朋友可以参考下
    2023-07-07
  • Python open()文件处理使用介绍

    Python open()文件处理使用介绍

    这篇文章主要介绍了Python open()文件处理使用介绍,需要的朋友可以参考下
    2014-11-11
  • wxPython 入门教程

    wxPython 入门教程

    您可以在几分钟内编写一段 Python脚本和让桌面拥有令人难以置信的相当漂亮的 GUI应用程序。这篇文章向您展示如何使用一 Python-著称的 GUI 库wxPython,来做到这一点的。向您的朋友和邻居介绍!
    2008-10-10
  • 将matplotlib绘图嵌入pyqt的方法示例

    将matplotlib绘图嵌入pyqt的方法示例

    这篇文章主要介绍了将matplotlib绘图嵌入pyqt的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-01-01
  • Python中glob类的使用方法

    Python中glob类的使用方法

    Python内置glob模块是一个操作文件的相关模块,由于模块功能比较少,很容易掌握,这篇文章主要介绍了Python中glob类的使用,需要的朋友可以参考下
    2022-12-12
  • python基础教程之常用运算符

    python基础教程之常用运算符

    这篇文章主要介绍了python基础教程之常用运算符,包含数学运算符、用于判断的运算符、逻辑运算符等,需要的朋友可以参考下
    2014-08-08
  • Python类的基本写法与注释风格介绍

    Python类的基本写法与注释风格介绍

    这篇文章主要介绍了Python类的基本写法与注释风格,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • 四步教你学会打包一个新的Python模块

    四步教你学会打包一个新的Python模块

    当你安装应用程序时,通常是安装一个软件包,其中包含应用程序的可执行代码和重要文件。在 Linux上,软件一般被打包成RPM或DEB等格式,然而几乎每天都有新的Python模块发布,因此你很容易遇到一个尚未打包的Python模块。本文教你四步打包一个新的Python模块
    2022-09-09
  • 基于Python实现简易学生信息管理系统

    基于Python实现简易学生信息管理系统

    这篇文章主要为大家详细介绍了python实现简易学生信息管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-07-07

最新评论