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网络编程学习笔记(七):HTML和XHTML解析(HTMLParser、BeautifulSoup)
Jun 09 Python
Python 实现引用其他.py文件中的类和类的方法
Apr 29 Python
Anaconda2下实现Python2.7和Python3.5的共存方法
Jun 11 Python
python Django 创建应用过程图示详解
Jul 29 Python
Python编程学习之如何判断3个数的大小
Aug 07 Python
Python datetime包函数简单介绍
Aug 28 Python
Python目录和文件处理总结详解
Sep 02 Python
Python 时间戳之获取整点凌晨时间戳的操作方法
Jan 28 Python
在matplotlib中改变figure的布局和大小实例
Apr 23 Python
selenium切换标签页解决get超时问题的完整代码
Aug 30 Python
Python全局变量与global关键字常见错误解决方案
Oct 05 Python
python中requests模拟登录的三种方式(携带cookie/session进行请求网站)
Nov 17 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 中执行系统外部命令
2006/10/09 PHP
ThinkPHP实现跨模块调用操作方法概述
2014/06/20 PHP
Zend Framework教程之响应对象的封装Zend_Controller_Response实例详解
2016/03/07 PHP
PHP获取用户访问IP地址的5种方法
2016/05/16 PHP
php实现的中秋博饼游戏之绘制骰子图案功能示例
2017/11/06 PHP
通过jquery实现tab标签浏览效果
2007/02/20 Javascript
window.parent调用父框架时 ie跟火狐不兼容问题
2009/07/30 Javascript
jquery获取input表单值的代码
2010/04/19 Javascript
JQuery中对Select的option项的添加、删除、取值
2013/08/25 Javascript
简介JavaScript中的sub()方法的使用
2015/06/08 Javascript
微信小程序  网络请求API详解
2016/10/25 Javascript
详解Vue爬坑之vuex初识
2017/06/14 Javascript
vue 数组和对象不能直接赋值情况和解决方法(推荐)
2017/10/25 Javascript
layui的表单验证支持ajax判断用户名是否重复的实例
2019/09/06 Javascript
vue 翻页组件vue-flip-page效果
2020/02/05 Javascript
node使用async_hooks模块进行请求追踪
2021/01/28 Javascript
在Python编程过程中用单元测试法调试代码的介绍
2015/04/02 Python
python常用知识梳理(必看篇)
2017/03/23 Python
Python实现按学生年龄排序的实际问题详解
2017/08/29 Python
python timestamp和datetime之间转换详解
2017/12/11 Python
Python最火、R极具潜力 2017机器学习调查报告
2017/12/11 Python
详解flask表单提交的两种方式
2018/07/21 Python
TensorFlow实现模型评估
2018/09/07 Python
如何基于python实现画不同品种的樱花树
2020/01/03 Python
谈一谈数组拼接tf.concat()和np.concatenate()的区别
2020/02/07 Python
Python正则表达式高级使用方法汇总
2020/06/18 Python
详解python 条件语句和while循环的实例代码
2020/12/28 Python
美国最大的购物网站:Amazon.com(亚马逊美国)
2020/05/23 全球购物
城建学院毕业生自荐信
2014/01/31 职场文书
酒店节能降耗方案
2014/05/08 职场文书
一年级小学生评语大全
2014/12/25 职场文书
升职感谢信
2015/01/22 职场文书
民事诉讼代理词
2015/05/25 职场文书
MySQL GRANT用户授权的实现
2021/06/18 MySQL
Node与Python 双向通信的实现代码
2021/07/16 Javascript
JavaScript实现简单的音乐播放器
2022/08/14 Javascript