#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2006
    Posts
    63
    Rep Power
    10

    Print to same line in console, over previous text?


    I'm trying to print to the same line in a console window, over the previous text. I know that adding a comma prints to the same line, but that prints after the previous item, where I'm trying to print over the previous item.

    For example, after the following example executes, we have "hello there" in the console window:

    print "hello ",
    print "there"

    Is there some way to have both these print, and be left with only "there", and no trace of the word "hello"?

    I'm trying to print a variable once per second in the console window, but not have any scroll. Sort of a poor man's gui effect.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Posts
    53
    Rep Power
    12
    i've never done any curses programming, but i know that's one way to accomplish what you want. python has a curses module, here's a tutorial:

    http://www.amk.ca/python/howto/curses/
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    52
    Hi!

    And without curses:
    Python Code:
    import sys
    import time
     
    for i in ["Hi", "You", "Fool"]:
        sys.stdout.write("\r" + i)
        sys.stdout.flush()
        time.sleep(1)


    Regards, mawe

    Comments on this post

    • Yegg` agrees
    • CountVikernes agrees
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2006
    Posts
    63
    Rep Power
    10
    Fantastic, that "\r" in conjunction with "," is just what I was looking for, thanks.

    Note that the stdout.write statements aren't really necessary. This is simpler, and has the advantage of not totally clearing the console window:

    Code:
    import sys
    import time
    
    print "this text will stay"
    
    print "but this will be \r",
    time.sleep(1)
    print "overwritten"
    time.sleep(1)

    Or, to make sure the line gets completely cleared, something cheesy like:

    Code:
    import sys
    import time
    
    print "this text will stay"
    
    print "but this will be \r",
    time.sleep(1)
    print "                                          \r", #this will erase the previous line
    print "overwritten"
    time.sleep(1)
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    52
    @wrybread: This works? When I try your code, I just see a blank line, no text (on Linux).
    Originally Posted by wrybread
    and has the advantage of not totally clearing the console window
    Hm? sys.stdout.write("\r") clears you console window?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2006
    Location
    Missouri
    Posts
    59
    Rep Power
    9
    Originally Posted by mawe
    @wrybread: This works? When I try your code, I just see a blank line, no text (on Linux).

    Hm? sys.stdout.write("\r") clears you console window?
    '\r' means carriage return
    sys.stdout.write("\r") doesn't clear your window it returns the cursor to the beginning of the current line.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    "\b" would do a backspace...

    how about using os.system("cls") on windows boxes... or anyother way to call command line functions
    Those people who think they know everything are a great annoyance to those of us who do.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    52
    Nathan87, monkeyman Yes I know what \r and \b do, but
    Originally Posted by wrybread
    This is simpler, and has the advantage of not totally clearing the console window
    Maybe I understood him wrong

IMN logo majestic logo threadwatch logo seochat tools logo