简体   繁体   English

Python中的动态单行打印(时间?)

[英]Dynamic single-line printing in Python (time?)

I'd like to make a simple clock (CLI) that prints the time into one line, and updates it every second. 我想制作一个简单的时钟(CLI),将时间打印成一行,并每秒更新一次。 Is this even possible? 这甚至可能吗? Should I just print a new line every second? 我应该每秒打印一行吗?

This is what I have at the moment, which functions terribly: 这就是我目前所拥有的,它的功能非常强大:

import calendar, time

a = 1
while a == 1:
    print (calendar.timegm(time.gmtime()))

print function print newline ( \\n ) after the string you passed. print函数在传递的字符串后打印换行符( \\n )。 Specify carriage return ( \\r ) explicitly does what you want. 指定回车符( \\r )显式执行您想要的操作。

To print every second, call time.sleep(1) after printing. 要每秒打印一次,请在打印后调用time.sleep(1)

import calendar
import time

while 1:
    print(calendar.timegm(time.gmtime()), end='\r')
    time.sleep(1)

UPDATE UPDATE

To make cursor remains at the end of the line, prepend \\r : 要使光标保持在行的末尾,请前置\\r

print('\r', calendar.timegm(time.gmtime()), sep='', end='')

If I understand, what you want to do is write the time, then, a second later, overwrite it with the new time, and so on. 如果我理解,你想要做的是写时间,然后,一秒钟后,用新时间覆盖它,依此类推。

On most terminals, printing a carriage return without a newline will take you back to the start of the same line. 在大多数终端上,打印没有换行符的回车将带您回到同一行的开头。 So, you can almost just do this: 所以,你几乎可以这样做:

print('\r{}'.format(calendar.timegm(time.gmtime())), end='')

In general, there's a problem with this: the carriage return doesn't erase the existing text, it just lets you overwrite it. 一般来说,这有一个问题:回车不会删除现有文本,它只是让你覆盖它。 So, what happens if the new value is shorter than the old one? 那么,如果新值比旧值短,会发生什么? Well, in your case, that isn't possible; 那么,在你的情况下,这是不可能的; you're printing a 10-digit number that can never turn into a 9-digit number. 您正在打印一个10位数字,永远不会变成9位数字。 But if it were a problem, the easiest solution would be to change that {} to something like {<70} , which will pad a short line with spaces, up to 70 characters. 但如果它一个问题,最简单的解决方案是将{}更改为{<70} ,这将填充一个带空格的短行,最多70个字符。 (Of course if your lines could be longer than 70 character, or your terminal could be narrower than 70, don't use that number.) (当然,如果您的线路长度超过70个字符,或者您的终端可能比70个更窄,请不要使用该号码。)


Meanwhile, if you just do this over and over as fast as possible, you're wasting a lot of CPU and I/O, and possibly screwing up your terminal's scrollback buffer, and who knows what else. 同时,如果你只是尽快反复这样做,你就会浪费大量的CPU和I / O,并可能搞砸终端的回滚缓冲区,谁知道还有什么。 If you want to do this once per second, you should sleep for a second in between. 如果你想每秒做一次,你应该在两者之间sleep

So: 所以:

while True:
    print('\r{}'.format(calendar.timegm(time.gmtime())))
    time.sleep(1)

If you want to get fancy, you can take over the whole terminal with curses on most non-Windows platforms, msvcrt console I/O on Windows, or even manually printing out terminal escape sequences. 如果你想得到花哨的话,你可以在大多数非Windows平台上使用curses接管整个终端,在Windows上使用msvcrt控制台I / O,甚至可以手动打印终端转义序列。 But you probably don't want to get fancy. 但你可能不想得到幻想。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM