1

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))
artless noise
  • 21,212
  • 6
  • 68
  • 105
user3291404
  • 781
  • 1
  • 5
  • 5

4 Answers4

0

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.

Community
  • 1
  • 1
Mahadeva
  • 1,584
  • 4
  • 23
  • 56
0

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.

MattDMo
  • 100,794
  • 21
  • 241
  • 231
0

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

-1

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)