[Tutor] Printing in the same place

Alan Gauld alan.gauld at btinternet.com
Wed Aug 17 10:02:04 CEST 2011


On 17/08/11 04:05, brandon w wrote:
> I am trying to print in the same place to make a clock in a tkinter
> window. I will loop the following code to update the time.
This is not a tkinter program so its completely irrelevant to your
stated aim. In tkinter  how print works is of no importance. You will
not be using print in a tkinter program you will simply be changing
the text in a widget.

> This seems to work but it is not printing in the same place:
>
> #!/usr/bin/python
> #Python 2.6.6
>
> import time
>
> for t in range(5):
> digits = time.strftime('%H:%M:%S')
> print "\r", digits
> time.sleep(0.1)

That's because you are using \r which is platform dependant
(and even terminal dependant on *nix) . You don't say which
platform you are using. Using \b (backspace) may be more
successful.

 >>> print ("test","\b\b\b\b\b\b","Foo")
  Foo

But, as I say, that won't be very useful in tkinter since you
won't be using print().

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list