使用python怎样写一个简单倒计时功能?
Admin 2021-08-18 群英技术资讯 1126 次浏览
怎样用Python写一个简单倒计时功能?对于倒计时功能的应用场景有很多,我们经常能在电商网站上看到,倒计时功能也是比较实用的,下面我们就来看看用Python怎样实现简单的倒计时。
使用tkinter制作界面实现倒计时功能。

代码如下:
#!/usr/bin/python3.8
# -*- coding: utf-8 -*-
# @Time : 2021/4/19 14:09
# @Author : dongdong
# @File : CountdownGUI.py
# @Software: PyCharm
from tkinter import *
import time
import threading
def cyclethread():
global counttime
global restartflag
global runflag
restartflag=False
if (timestr.get().isdigit()):
counttime = int(timestr.get()) * 60
else:
runflag=False
return;
while (1):
if(restartflag):
counttime = int(timestr.get()) * 60
restartflag=False
if(exitflag):
sys.exit()
counttime=counttime-1
v='\nleft time:'+str(counttime//60)+' :'+str(counttime%60)
textshow.set(v)
root.update()
if (counttime <= 0):
runflag = False
return
time.sleep(1)
def startCount():
global restartflag
global runflag
restartflag=True
if( not runflag):
th=threading.Thread(target=cyclethread)
th.setDaemon(True)
th.start()
runflag = True
def exitfun():
global exitflag
exitflag=True
sys.exit()
restartflag=False
exitflag=False
counttime=None
runflag=False
root=Tk()
root.geometry('250x120')
root.title('TimeCounter')
timestr = StringVar(value="30")
textshow=StringVar(value='\nCountDown:30min ')
text0=Label(root,text='Input time(min):').grid(row=0,column=0,columnspan=3)
entext=Entry(root,textvariable=timestr).grid(row=0,column=3,columnspan=1)
# bnframe=ttk.Frame(root).grid(row=1,column=0,columnspan=4)
stbn=Button(root,text='Start',command=startCount).grid(row=1,column=2,columnspan=1)
enbn=Button(root,text='Exit',command=exitfun).grid(row=1,column=3,columnspan=1)
text=Label(root,textvariable=textshow).grid(row=2,column=0,columnspan=4)
root.mainloop()
以上就是关于python写简单倒计时功能的代码了,上述代码具有一定的学习和参考价值,希望能对大家学习python有帮助,想要了解更多python实现其他功能,大家可以继续关注其他文章。
文本转载自脚本之家
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了python中常见进制之间的转换方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
这篇文章主要为大家详细介绍了如何利用Python语言绘制好看的数据动态图,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起动手尝试一下
当初学 Python 时,想要弄懂 Python 的错误信息的含义可能有点复杂。这里列出了常见的的一些让你程序 crash 的运行时错误。1)忘记在...
这篇文章主要介绍了python+pytest接口自动化参数关联,参数关联,也叫接口关联,即接口之间存在参数的联系或依赖,更多相关内容需要的小伙伴可可以参考一下
break语句用于提前终止当前循环。放弃循环后,直接执行循环块下一个语句,就像C语言中的break语句一样。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008