python 实现倒计时功能(gui界面)

 更新时间:2020年11月11日 11:35:12   作者:TechVidvan  
这篇文章主要介绍了python 实现倒计时功能(gui界面),帮助大家更好的理解和使用python,感兴趣的朋友可以了解下

运行效果:

完整源码:

##import library
from tkinter import *
import time
from playsound import playsound


## display window 
root = Tk()
root.geometry('400x300')
root.resizable(0,0)
root.config(bg ='blanched almond')
root.title('TechVidvan - Countdown Clock And Timer')
Label(root, text = 'Countdown Clock and Timer' , font = 'arial 20 bold', bg ='papaya whip').pack()


#display current time#######################

Label(root, font ='arial 15 bold', text = 'current time :', bg = 'papaya whip').place(x = 40 ,y = 70)


####fun to display current time
def clock():
 clock_time = time.strftime('%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 = 190 , y = 70)
clock()


#######################timer countdown##########


#storing seconds
sec = StringVar()
Entry(root, textvariable = sec, width = 2, font = 'arial 12').place(x=250, y=155)
sec.set('00')

#storing minutes
mins= StringVar()
Entry(root, textvariable = mins, width =2, font = 'arial 12').place(x=225, y=155)
mins.set('00')


# storing hours
hrs= StringVar()
Entry(root, textvariable = hrs, width =2, font = 'arial 12').place(x=200, y=155)
hrs.set('00')

##########fun to start countdown

def countdown():
 times = int(hrs.get())*3600+ int(mins.get())*60 + int(sec.get())
 while times > -1:
  minute,second = (times // 60 , times % 60)
  
  hour = 0
  if minute > 60:
   hour , minute = (minute // 60 , minute % 60)
   
  sec.set(second)
  mins.set(minute)
  hrs.set(hour)
  
  root.update()
  time.sleep(1)

  if(times == 0):
   playsound('Loud_Alarm_Clock_Buzzer.mp3')
   sec.set('00')
   mins.set('00')
   hrs.set('00')
  times -= 1

Label(root, font ='arial 15 bold', text = 'set the time', bg ='papaya whip').place(x = 40 ,y = 150)

Button(root, text='START', bd ='5', command = countdown, bg = 'antique white', font = 'arial 10 bold').place(x=150, y=210)
  


root.mainloop()

想要获得更多关于python的资讯、工具、实例,请关注python客栈

以上就是python 实现倒计时功能(gui界面)的详细内容,更多关于python 倒计时的资料请关注脚本之家其它相关文章!

相关文章

  • Python使用Kubernetes API访问集群

    Python使用Kubernetes API访问集群

    本文主要介绍了Python使用Kubernetes API访问集群,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-05-05
  • pytorch中tensor的合并与截取方法

    pytorch中tensor的合并与截取方法

    今天小编就为大家分享一篇pytorch中tensor的合并与截取方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • python 数据清洗之数据合并、转换、过滤、排序

    python 数据清洗之数据合并、转换、过滤、排序

    这篇文章主要介绍了python 数据清洗之数据合并、转换、过滤、排序的相关资料,需要的朋友可以参考下
    2017-02-02
  • python Tornado异步使用场景源码解析

    python Tornado异步使用场景源码解析

    这篇文章主要为大家介绍了python Tornado异步使用场景源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-09-09
  • pandas数据处理清洗实现中文地址拆分案例

    pandas数据处理清洗实现中文地址拆分案例

    因为后续数据分析工作需要用到地理维度进行分析,所以需要把login_place字段进行拆分成:国家、省份、地区。感兴趣的可以了解一下
    2021-06-06
  • 如何在python开发工具PyCharm中搭建QtPy环境(教程详解)

    如何在python开发工具PyCharm中搭建QtPy环境(教程详解)

    这篇文章主要介绍了在python开发工具PyCharm中搭建QtPy环境,本文通过图文并茂的形式给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-02-02
  • Python中实现ipaddress网络地址的处理

    Python中实现ipaddress网络地址的处理

    ipaddress库提供了处理IPv4与IPv6网络地址的类。这些类支持验证,查找网络上的地址和主机,以及其他常见的操作,本文就来介绍一下这些方法的使用,感兴趣的一起来了解一下
    2021-06-06
  • python 消费 kafka 数据教程

    python 消费 kafka 数据教程

    今天小编就为大家分享一篇python 消费 kafka 数据教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-12-12
  • numpy数组广播的机制

    numpy数组广播的机制

    这篇文章主要介绍了numpy数组广播的机制,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07
  • PyQt5+QtChart实现绘制曲线图

    PyQt5+QtChart实现绘制曲线图

    QChart是一个QGraphicScene中可以显示的QGraphicsWidget。本文将利用QtChart实现曲线图的绘制,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
    2022-12-12

最新评论