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


Posted in Python onNovember 11, 2020

运行效果:

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

完整源码:

##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 实现倒计时功能(gui界面)的详细内容,更多关于python 倒计时的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
python中使用urllib2获取http请求状态码的代码例子
Jul 07 Python
使用IronPython把Python脚本集成到.NET程序中的教程
Mar 31 Python
python通过加号运算符操作列表的方法
Jul 28 Python
如何准确判断请求是搜索引擎爬虫(蜘蛛)发出的请求
Oct 13 Python
Python3.4学习笔记之常用操作符,条件分支和循环用法示例
Mar 01 Python
利用Python实现微信找房机器人实例教程
Mar 10 Python
Python3.6实现带有简单界面的有道翻译小程序
Apr 16 Python
Python实现TCP通信的示例代码
Sep 09 Python
关于初始种子自动选取的区域生长实例(python+opencv)
Jan 16 Python
Pytorch 使用CNN图像分类的实现
Jun 16 Python
Python OpenCV读取中文路径图像的方法
Jul 02 Python
浅谈Python 中的复数问题
May 19 Python
windows+vscode安装paddleOCR运行环境的步骤
Nov 11 #Python
Django基于Models定制Admin后台实现过程解析
Nov 11 #Python
Django Admin后台模型列表页面如何添加自定义操作按钮
Nov 11 #Python
Django启动时找不到mysqlclient问题解决方案
Nov 11 #Python
Python+logging输出到屏幕将log日志写入文件
Nov 11 #Python
通过Django Admin+HttpRunner1.5.6实现简易接口测试平台
Nov 11 #Python
Django自定义YamlField实现过程解析
Nov 11 #Python
You might like
PHP实现无限极分类图文教程
2014/11/25 PHP
PHP调用MySQL存储过程并返回值的方法
2014/12/26 PHP
PHP使用JSON和将json还原成数组
2015/02/12 PHP
PHP常用工具类大全附全部代码下载
2015/12/07 PHP
PHP转换文本框内容为HTML格式的方法
2016/07/20 PHP
htm调用JS代码
2007/03/15 Javascript
javascript加号"+"的二义性说明
2013/03/04 Javascript
jquery $(this).attr $(this).val方法使用介绍
2013/10/08 Javascript
JS读取XML文件示例代码
2013/11/15 Javascript
js获取域名的方法
2015/01/27 Javascript
js替换字符串中所有指定的字符(实现代码)
2016/08/17 Javascript
浅谈jquery中next与siblings的区别
2016/10/27 Javascript
ES6新特性一: let和const命令详解
2017/04/20 Javascript
判断jQuery是否加载完成,没完成继续判断的解决方法
2017/12/06 jQuery
vue 国际化 vue-i18n 双语言 语言包
2018/06/07 Javascript
React中使用async validator进行表单验证的实例代码
2018/08/17 Javascript
[01:48]2018DOTA2亚洲邀请赛主赛事第二日五佳镜头 VG完美团战逆转TNC
2018/04/05 DOTA
[43:14]Liquid vs Optic 2018国际邀请赛淘汰赛BO3 第二场 8.21
2018/08/22 DOTA
Python第三方库的安装方法总结
2016/06/06 Python
一个基于flask的web应用诞生 flask和mysql相连(4)
2017/04/11 Python
Scrapy爬虫实例讲解_校花网
2017/10/23 Python
python3.6生成器yield用法实例分析
2019/08/23 Python
使用python将excel数据导入数据库过程详解
2019/08/27 Python
Python类的绑定方法和非绑定方法实例解析
2020/03/04 Python
python 实现端口扫描工具
2020/12/18 Python
Python3中的tuple函数知识点讲解
2021/01/03 Python
DC Shoes荷兰官方网站:美国极限运动品牌
2019/10/22 全球购物
英国最大的在线亚洲杂货店:Red Rickshaw
2020/03/22 全球购物
Python中pass语句的作用是什么
2016/06/01 面试题
中学生个人自我评价
2014/02/06 职场文书
小学新学期寄语
2014/04/02 职场文书
春节慰问信范文
2015/02/15 职场文书
2016中学教师读书心得体会
2016/01/13 职场文书
纯html+css实现Element loading效果
2021/08/02 HTML / CSS
一文了解MySQL二级索引的查询过程
2022/02/24 MySQL
VUE中的v-if与v-show区别介绍
2022/03/13 Vue.js