How to make a countdown clock in Python on Sublime Text 2

I'm looking to create a countdown clock using Python on Sublime Text 2. I've gotten it to the point where it will display:

01:00 00:59 00:58 ... 00:00

However, I want the timer to be displayed only in one position so it looks like a continuous countdown. I've tried numerous ways but can't seem to get the line to be overwritten in Sublime. Here is the code I have so far:

def countdown(p,q):
    i=p
    j=q
    k=0
    while True:
        if(j==-1):
            j=59
            i -=1
        if(j > 9):  
            print(str(k)+str(i)+":"+str(j), end='\r'),
        else:
            print(str(k)+str(i)+":"+str(k)+str(j) , end='\r'),
        time.sleep(1)
        j -= 1
        if(i==0 and j==-1):
            break
    if(i==0 and j==-1):
        print("Goodbye!", end="\r")
        time.sleep(1)
print(countdown(1,0))

Answers


As far as I know, sublime has no method to clear the console. The code given below will display the result you need in terminal.

import time,os
def countdown(p,q):
    i=p
    j=q
    k=0
    while True:
        if(j==-1):
            j=59
            i -=1
        if(j > 9):  
            print str(k)+str(i) +":"+str(j),
        else:
            print str(k)+str(i)+":"+str(k)+str(j),
        time.sleep(1)
        j -= 1
        if(i==0 and j==-1):
            break
        os.system("cls") # or os.system("clear") if on linux
    if(i==0 and j==-1):
        os.system("cls")
        print "Goodbye!",
        time.sleep(1)
print countdown(1,0)

However, there is a hack given here. You can use print('\n'*100) to emulate the behavior of clearing of console.


The easiest method I can think of is to have your countdown running in the status bar. sublime.status_message() takes a string argument and displays it in the status bar at the bottom.

Also, instead of using time.sleep(), try using sublime.set_timeout(). Create a callback function that decreases the time by 1 second every time it's called. Then, use set_timeout(callback, delay=1000) to call it once a second (1000 milliseconds).

Finally, I'd strongly recommend you use semantic variables - name them according to what they are, instead of random single-letter designations. Python programs are meant to be easily readable and understood, and it's much easier to debug minutes, seconds, and counter instead of i, j, and k.


Just modified @Fabio Duran Verdugo code

            import time
            def countdown(p,q):
                k = 0
                a = ""

                while True:

                    if(q == -1):                                                              
                        q = 59 #default seconds for countdown
                        p -= 1 #decremental value for minutes

                    if(q > 9):
                       a = str(k) + str(p) + ":" + str(q) + "\n"
                    else:
                       a = str(k) + str(p) + ":" + str(k) + str(q) + "\n"
                    print (a)

                    time.sleep(1)
                    q -= 1
                    if(p == 0 and q == -1):
                        break

                if(p == 0 and q == -1):
                    print("Goodbye!") #print goodbye
                    time.sleep(10)    #timer before going to sleep (seconds)
            countdown(1,0) #format (minutes, seconds)

the code now doesn't print redundant lines(improved ui), made a few code documentation for easy flow up.

PS: I'm not taking any credit for this code :) happy learning guys


Concat the result and printing everytime the concat var.

import time                                                                     

def countdown(p,q):                                                             
    i=p                                                                         
    j=q                                                                         
    k=0                                                                         
    a=""                                                                        
    while True:                                                                 
        if(j==-1):                                                              
            j=59                                                                
            i -=1                                                               
        if(j > 9):                                                              
            a += str(k)+str(i)+":"+str(j) + " "                                 
        else:                                                                   
            a += str(k)+str(i)+":"+str(k)+str(j) + " "                                                                                              
        print (a, end='\r')                                                     
        time.sleep(1)                                                           
        j -= 1                                                                  
        if(i==0 and j==-1):                                                     
           break                                                               
    if(i==0 and j==-1):                                                         
        print("Goodbye!", '\r')                                                 
        time.sleep(1)                                                           
countdown(1,0) 

Need Your Help

Uncaught TypeError: a.getAttribute is not a function

javascript jquery zurb-foundation-5

I'm developing a site locally on ModX using Foundation5 as the framework, and employing Foundation's offcanvas menu. Offcanvas has functioned perfectly since its implementation a couple of weeks ago.

jQuery & IE on Refresh/Reload

jquery internet-explorer refresh reload

Basically I create a page with a jQuery scroller on it.