使用python怎样写一个简单倒计时功能?
Admin 2021-08-18 群英技术资讯 1102 次浏览
怎样用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基于ssh远程连接Mysql数据库操作示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
大家好,本篇文章主要讲的是linux中使用Python对图片进行批量命名,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
这篇文章主要介绍了Python 实现多表和工作簿合并及一表按列拆分,文章围绕主题展开详细的资料介绍,具有一定的参考价值,需要的小伙伴可以参考一下
之前小编向大家讲解了很多关于列表的知识,我们都知道在python中列表特别重要,也很好用。但是有的时候,我们仅仅需要对数字进行处理时,虽然列表也可以处理数字,但是相比较之下,用array(数组)更加高效一些,因为array(数组)适合仅仅对数字且是同一类型的数字操作。那你知道如何将列表转为数组处理数据吗?别急,今天小编就告诉大家python中array与list相互转换的方法。
这篇文章给大家分享的是有关python的textwrap库怎么用的内容,通过textwrap库进行操作,我们能更便捷的对多文本进行处理,小编觉得挺实用的,因此分享给大家做个参考,接下来一起跟随小编看看吧。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
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