溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python如何實(shí)現(xiàn)簡(jiǎn)單倒計(jì)時(shí)功能

發(fā)布時(shí)間:2021-04-27 10:29:56 來(lái)源:億速云 閱讀:347 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)python如何實(shí)現(xiàn)簡(jiǎn)單倒計(jì)時(shí)功能的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

python的五大特點(diǎn)是什么

python的五大特點(diǎn):1.簡(jiǎn)單易學(xué),開(kāi)發(fā)程序時(shí),專(zhuān)注的是解決問(wèn)題,而不是搞明白語(yǔ)言本身。2.面向?qū)ο?,與其他主要的語(yǔ)言如C++和Java相比, Python以一種非常強(qiáng)大又簡(jiǎn)單的方式實(shí)現(xiàn)面向?qū)ο缶幊獭?.可移植性,Python程序無(wú)需修改就可以在各種平臺(tái)上運(yùn)行。4.解釋性,Python語(yǔ)言寫(xiě)的程序不需要編譯成二進(jìn)制代碼,可以直接從源代碼運(yùn)行程序。5.開(kāi)源,Python是 FLOSS(自由/開(kāi)放源碼軟件)之一。

使用python實(shí)現(xiàn)簡(jiǎn)單倒計(jì)時(shí)exe,具體內(nèi)容如下

使用tkinter制作界面實(shí)現(xiàn)倒計(jì)時(shí)功能。

python如何實(shí)現(xiàn)簡(jiǎn)單倒計(jì)時(shí)功能

  • 使用time.sleep(1)實(shí)現(xiàn) 秒級(jí) 倒計(jì)時(shí)

  • 使用線程避免界面卡死

  • 在線程的循環(huán)中檢測(cè)全局標(biāo)志位,保證計(jì)時(shí)線程的重置、以及退出

  • 使用pyinstaller -F file.py -w 生成exe文件,-w表示隱藏控制臺(tái),-F表示生成單文件

代碼如下:

#!/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()

感謝各位的閱讀!關(guān)于“python如何實(shí)現(xiàn)簡(jiǎn)單倒計(jì)時(shí)功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI