在pythonshell中运行一个基本的数字时钟
我想在python shell中编写简单的数字时钟。如果可能的话,我想避免使用tkinter。这就是我现在拥有的在pythonshell中运行一个基本的数字时钟,python,shell,clock,digital,Python,Shell,Clock,Digital,我想在python shell中编写简单的数字时钟。如果可能的话,我想避免使用tkinter。这就是我现在拥有的 import time while True: from datetime import datetime now = datetime.now() print ("%s/%s/%s %s:%s:%s" % (now.month,now.day,now.year,now.hour,now.minute,now.second)) time.slee
import time
while True:
from datetime import datetime
now = datetime.now()
print ("%s/%s/%s %s:%s:%s" % (now.month,now.day,now.year,now.hour,now.minute,now.second))
time.sleep(1)
这会产生一个循环打印输出,类似这样的东西
06/29/16 23:08:32
06/29/16 23:08:33
06/29/16 23:08:34
我知道这很粗糙,我还在学习。我只想要一条线,壳里有一个“滴答”的数字钟。我在idle和windows10上使用python3.5.1
如果这不可能,我很想知道为什么
非常感谢如果您每次只是打印出这样一个固定长度的输出,您可以使用回车符倒带到行首,只要您不打印换行符。例如:
# Note trailing comma, that suppresses the newline in Python
print ("%s/%s/%s %s:%s:%s" % (now.month,now.day,now.year,now.hour,now.minute,now.second)),
# Now rewind back to the start of the line. Again, not trailing comma
print("\r"),
现在,您可能还注意到屏幕上从未打印任何内容。这是因为标准输出是缓冲的,所以您可以使用以下内容刷新:
# At the top...
import sys
# In the loop, after the first print
sys.stdout.flush()
这一切工作如下。假设屏幕上实际上有一个光标。首先用第一次打印(和刷新)打印出时间,然后用print(“\r”),
将光标移回行的开头。这实际上并没有删除任何字符,只是移动了光标。然后你再写下一次。因为它恰好是完全相同的长度,所以时间会再次被写出来,替换旧字符
生成的脚本如下所示:
import time
import sys
while True:
from datetime import datetime
now = datetime.now()
print ("%s/%s/%s %s:%s:%s" % (now.month,now.day,now.year,now.hour,now.minute,now.second)),
sys.stdout.flush()
print("\r"),
time.sleep(1)
如果您想要对正在发生的事情进行更细粒度的控制,可以开始使用curses库,但我认为这对于您在这里尝试做的事情来说是过分的
编辑:正如注释中提到的@PadraicCunningham,在Python 3中禁止换行打印并强制内容刷新到屏幕的正确语法如下:
print("hello", flush=True, end="")
此外,正如@AlexHall提到的,print语句实际上并不打印固定宽度的语句;为此,我们应该使用strftime()
因此,正确的程序是:
import time
while True:
from datetime import strftime
print (strftime("%m/%d/%Y %H:%M:%S"), end="", flush=True)
print("\r", end="", flush=True)
time.sleep(1)
您所需要的只是:
from time import strftime
while True:
print (strftime("%m/%d/%Y %H:%M:%S"), end="", flush=True)
print("\r", end="", flush=True)
time.sleep(1)
在repl.it中尝试了这个,这对我很有效…(添加了逗号&now.strftime)
以下代码适用于我
from time import sleep
from datetime import datetime
while True:
now = datetime.now()
stdout.write(now.strftime("\r%m/%d/%Y %H:%M:%S")),
stdout.flush()
sleep(1)
stdout.write("\n")
您可以设置end=”“
以删除换行符并添加回车符,即:
'\r'
因此,适合您的正确程序是:
from datetime import datetime
import time
while True:
now = datetime.now()
print("\r%s/%s/%s %s:%s:%s" % (now.month, now.day, now.year, now.hour, now.minute, now.second), end='')
time.sleep(1)
print('')
对上述答案的一点改进(删除尾随逗号;将end从“”更改为“\r”),以将回车作为单个打印语句的一部分
import time
from datetime import datetime
while True:
now = datetime.now()
print (now.strftime("%m/%d/%Y %H:%M:%S"), end="\r", flush=True)
time.sleep(1)
我不认为你在idle中会有太多的运气,我建议你完全忘记idle,使用ipython或pycharm等众多优秀python IDE之一。你可以在打印函数中设置
flush=True
,但我不认为你所做的任何事情都能奏效idle@PadraicCunningham:thanks,我没有意识到这是一个选项。我自己也不确定是否空闲,遗憾的是没有空闲时间进行测试。不过它在Python REPL中确实可以工作。不用担心,OP使用的是python3.5,因此您还可以设置end=“”
要从内存中删除换行符,我认为这在空闲状态下不起作用,我认为安装idlex可能会奏效,我仍然习惯于Python 2。不过更新了,干杯:)@AlexHall,哇,删除了.flush()
。至于要求flush=True
,这完全取决于您的平台。Linux和OS X的libc通常会缓冲输出,并且只在换行时刷新到屏幕上;我不能100%肯定Windows的行为。
import time
from datetime import datetime
while True:
now = datetime.now()
print (now.strftime("%m/%d/%Y %H:%M:%S"), end="\r", flush=True)
time.sleep(1)