SoFunction
Updated on 2024-11-17

Python implementation of a simple countdown function

A simple countdown exe using python is implemented for your reference as follows

Use tkinter to create an interface to implement the countdown function.

  • Use (1) to implement a seconds countdown.
  • Using threads to avoid interface jams
  • Detecting the global flag bit in the thread's loop ensures that the timed thread resets and exits.
  • Use pyinstaller -F -w to generate an exe file, -w to hide the console, -F to generate a single file

The code is as follows:

#!/usr/bin/python3.8
# -*- coding: utf-8 -*-
# @Time    : 2021/4/19 14:09
# @Author  : dongdong
# @File    : 
# @Software: PyCharm

from tkinter import *
import time
import threading
def cyclethread():
    global counttime
    global restartflag
    global runflag
    restartflag=False

    if (().isdigit()):
        counttime = int(()) * 60
    else:
        runflag=False
        return;
    while (1):
        if(restartflag):
            counttime = int(()) * 60
            restartflag=False
        if(exitflag):
            ()

        counttime=counttime-1
        v='\nleft time:'+str(counttime//60)+' :'+str(counttime%60)
        (v)
        ()
        if (counttime <= 0):
            runflag = False
            return
        (1)

def startCount():
    global  restartflag
    global runflag
    restartflag=True
    if( not runflag):
        th=(target=cyclethread)
        (True)
        ()
        runflag = True

def exitfun():
    global exitflag
    exitflag=True
    ()

restartflag=False
exitflag=False
counttime=None
runflag=False
root=Tk()
('250x120')
('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=(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)
()

This is the whole content of this article.