使用python怎样写一个简单倒计时功能?
Admin 2021-08-18 群英技术资讯 946 次浏览
怎样用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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
协程:英文名Coroutine,是单线程下的并发,又称微线程,纤程。协程是一种用户态的轻量级线程,即协程是由用户程序自己控制调度的。对比操作系统控制线程的切换,用户在单线程内控制协程的切换。协程自己本身无法实现并发(甚至性能会降低),协程+IO切换性能提高。
这篇文章主要介绍了Python自定义对象实现切片功能,__getitem__()魔术方法,并用于实现自定义对象(以列表类型和字典类型为例)的切片功能,需要的朋友可以参考一下
求最大公约数是习题中比较常见的类型,本文小编将给大家提供五种比较常见的算法,都是用Python语言实现的,感兴趣的小伙伴可以了解一下
Python提供了许多操作Excel的模块,能够让我们从繁琐的工作中腾出双手。本文主要为大家介绍的是openpyxl模块,它的功能相对与其他模块更为齐全,感兴趣的小伙伴快来学习一下吧
在一般情况下,语句是顺序执行的:在一个函数的第一条语句,首先执行,然后是第二条,第三条...等等。可能有一种情况,当你需要执行代码块几次或多数。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
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备09006778号 域名注册商资质 粤 D3.1-20240008