如何通过覆盖NTP服务器先前收到的Timing(倒计时)来输出到同一行.如下所示,在每个第二时序之后接收下一行.
13:35:01
13:35:00
13:34:59
13:34:58
13:34:57
13:34:56
Run Code Online (Sandbox Code Playgroud)
我希望时间应该在同一行清除前一个.
tob*_*s_k 28
您可以使用"return"字符\r返回到行的开头.在Python 2.x中,您将不得不使用sys.stdout.write而sys.stdout.flush不是print.
import time, sys
while True:
sys.stdout.write("\r" + time.ctime())
sys.stdout.flush()
time.sleep(1)
Run Code Online (Sandbox Code Playgroud)
在Python 3.3中,您可以使用print函数,with end和flush参数:
print(time.ctime(), end="\r", flush=True)
Run Code Online (Sandbox Code Playgroud)
但请注意,这样您只能替换屏幕上的最后一行.如果您想在更复杂的仅限控制台的UI中使用"实时"时钟,则应该检查curses.
import time, curses
scr = curses.initscr()
scr.addstr(0, 0, "Current Time:")
scr.addstr(2, 0, "Hello World!")
while True:
scr.addstr(0, 20, time.ctime())
scr.refresh()
time.sleep(1)
Run Code Online (Sandbox Code Playgroud)
小智 5
这将像冠军一样工作:
print("This text is about to vanish - from first line", end='')
print("\rSame line output by Thirumalai")
Run Code Online (Sandbox Code Playgroud)
输出:
Thirumalai 从第一行输出的同一行
| 归档时间: |
|
| 查看次数: |
21776 次 |
| 最近记录: |