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 2.7.x 和 3.x 版本的重要区别小结
Nov 28 Python
python实现连接mongodb的方法
May 08 Python
安装Python的教程-Windows
Jul 22 Python
20个常用Python运维库和模块
Feb 12 Python
tensorflow获取变量维度信息
Mar 10 Python
Python数据处理numpy.median的实例讲解
Apr 02 Python
深入浅析python with语句简介
Apr 11 Python
django 发送邮件和缓存的实现代码
Jul 18 Python
python使用正则筛选信用卡
Jan 27 Python
Pycharm远程连接服务器并实现代码同步上传更新功能
Feb 25 Python
Python实现寻找回文数字过程解析
Jun 09 Python
Python 获取异常(Exception)信息的几种方法
Dec 29 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
实用函数9
2007/11/08 PHP
php intval的测试代码发现问题
2008/07/27 PHP
Php获取金书网的书名的实现代码
2010/06/11 PHP
PHP中call_user_func_array回调函数的用法示例
2016/11/26 PHP
PHP进制转换实例分析(2,8,16,36,64进制至10进制相互转换)
2017/02/04 PHP
如何通过View::first使用Laravel Blade的动态模板详解
2017/09/21 PHP
PHP实现动态创建XML文档的方法
2018/03/30 PHP
ThinkPHP框架实现的MySQL数据库备份功能示例
2018/05/24 PHP
ThinkPHP3.2框架自带分页功能实现方法示例
2019/05/13 PHP
关于laravel5.5的定时任务详解(demo)
2019/10/23 PHP
JavaScript中链式调用之研习
2011/04/07 Javascript
js给onclick事件赋值,动态传参数实例解说
2013/03/28 Javascript
jquery $.trim()方法使用介绍
2014/05/21 Javascript
JS实现判断碰撞的方法
2015/02/11 Javascript
JavaScript使ifram跨域相互访问及与PHP通信的实例
2016/03/03 Javascript
基于javascript显示当前时间以及倒计时功能
2016/03/18 Javascript
JavaScript验证知识整理
2017/03/24 Javascript
详解Vue.js Mixins 混入使用
2017/09/15 Javascript
详解Vue 多级组件透传新方法provide/inject
2018/05/09 Javascript
vue如何解决循环引用组件报错的问题
2018/09/22 Javascript
vue-cli3 项目从搭建优化到docker部署的方法
2019/01/28 Javascript
vue 路由子组件created和mounted不起作用的解决方法
2019/11/05 Javascript
vue实现前端列表多条件筛选
2020/10/26 Javascript
windows 下python+numpy安装实用教程
2017/12/23 Python
python3获取当前文件的上一级目录实例
2018/04/26 Python
Python Django 前后端分离 API的方法
2019/08/28 Python
美国在线印刷公司:PsPrint
2017/10/12 全球购物
Noon埃及:埃及在线购物
2019/11/26 全球购物
通息工程毕业生自荐信
2013/10/16 职场文书
英语道歉信范文
2014/01/09 职场文书
高三复习计划
2015/01/19 职场文书
庆六一开幕词
2015/01/29 职场文书
公司庆典主持词
2015/07/04 职场文书
公司管理建议书
2015/09/14 职场文书
汉语拼音教学反思
2016/02/22 职场文书
Golang连接并操作MySQL
2022/04/14 MySQL