python 实现倒计时功能(gui界面)
作者:TechVidvan 时间:2021-03-05 14:19:55
运行效果:
完整源码:
##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客栈
来源:https://techvidvan.com/tutorials/countdown-clock-timer-python/
标签:python,倒计时,gui
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
动易CMS:九个常见的错误原因分析及解决方法(ZT)
2008-07-01 12:40:00
SQL Server各种日期计算方法
2008-09-11 21:47:00
python 读取、写入txt文件的示例
2023-09-29 22:04:18
![](https://img.aspxhome.com/file/2023/7/63237_0s.png)
三种SQL分页法
2010-05-07 11:03:00
简要介绍SQL Server 2008新的事件处理系统
2009-12-22 08:15:00
![](https://img.aspxhome.com/file/UploadPic/20101/11/210u51y4y-40s.jpg)
conn.execute(sql)后面加(0)和不加的区别
2009-06-24 11:13:00
PHP PDOStatement::debugDumpParams讲解
2023-06-06 04:18:55
运行SQL Server的计算机间移动数据库
2009-01-20 13:07:00
accept-charset与Header P3P
2009-04-01 18:43:00
如何解决客户机页面刷新时连接不上数据库问题?
2009-12-16 18:24:00
一文带你熟悉Go语言中函数的使用
2023-07-13 08:22:23
百度编辑器复制微信图片无法保存
2023-08-14 17:32:46
![](https://img.aspxhome.com/file/202308/1692005789643522s.png)
深入浅出SQL嵌套SELECT语句
2009-02-06 14:25:00
丰富段落里的标签
2008-03-16 14:11:00
详解php实现页面静态化原理
2023-10-25 10:58:10
js星级评分代码
2008-05-02 21:07:00
![](https://img.aspxhome.com/file/UploadPic/20085/2/star_78s.gif)
高考要来啦!用Python爬取历年高考数据并分析
2021-06-10 04:08:25
![](https://img.aspxhome.com/file/2023/9/68439_0s.jpg)
详解Python静态网页爬取获取高清壁纸
2023-10-18 08:27:42
![](https://img.aspxhome.com/file/2023/4/63214_0s.png)
什么是XSLT,什么是XPath
2008-01-21 13:12:00
Golang设计模式中抽象工厂模式详细讲解
2023-08-04 20:46:46
![](https://img.aspxhome.com/file/2023/4/93664_0s.png)