import sys
import time
import threading
def print_act():
while True:
sys.stdout.write('\r')
sys.stdout.flush()
for item in '请勿越过白色安全线':
sys.stdout.write(item)
sys.stdout.flush()
time.sleep(0.3)
def print_time():
while True:
sys.stdout.write("\r")
sys.stdout.flush()
sys.stdout.write('\033[1;31m'+time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())+'\033[0m')
time.sleep(0.5)
print('\033[1;31m'+'新春佳节快乐'+'\033[0m')
threads = []
t1 = threading.Thread(target=print_act)
threads.append(t1)
t2 = threading.Thread(target=print_time)
threads.append(t2)
for t in threads:
t.start()
for t in threads:
t.join()请问下代码出现了什么问题,导致两行文字的动态效果串行了。
公子羽爱猫 发表于2020-02-24 22:55
那请问下应该怎么修改呢?
我研究了一下,可以改写成这样:
import sys
import os
import time
text = list('请勿越过白色安全线')
times = 1
while True:
os.system("cls")
sys.stdout.write('新春佳节快乐\n')
sys.stdout.flush()
sys.stdout.write(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()))
sys.stdout.flush()
sys.stdout.write("\n")
if not times % 10 == 0:
sys.stdout.write("\r")
sys.stdout.flush()
for i in range(times % 10):
sys.stdout.write(text[i])
sys.stdout.flush()
else:
sys.stdout.write("\r")
sys.stdout.write(" ")
sys.stdout.flush()
time.sleep(1)
times += 1
不过只能在cmd.exe、py.exe、python.exe中正常运行
hello_world 发表于2020-02-26 12:17
我研究了一下,可以改写成这样:
import sys
import os
import time
text = list('请勿越过白色安全线')
times = 1
while True:
os.system("cls")
sys.stdout.write('新春佳节快乐\n')
sys.stdout.flush()
sys.stdout.write(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()))
sys.stdout.flush()
sys.stdout.write("\n")
if not times % 10 == 0:
sys.stdout.write("\r")
sys.stdout.flush()
for i in range(times % 10):
sys.stdout.write(text[i])
sys.stdout.flush()
else:
sys.stdout.write("\r")
sys.stdout.write(" ")
sys.stdout.flush()
time.sleep(1)
times += 1
不过只能在cmd.exe、py.exe、python.exe中正常运行
请勿越过白色安全线应该在第二行,而时间是第三行,运行了一下,显示出的效果不对。题目要求的效果如图:

公子羽爱猫 发表于2020-02-27 03:11
请勿越过白色安全线应该在第二行,而时间是第三行,运行了一下,显示出的效果不对。题目要求的效果如图:

不好意思,没注意。这样就行了:
import sys
import os
import time
text = list('请勿越过白色安全线')
times = 1
while True:
os.system("cls")
sys.stdout.write('新春佳节快乐\n')
sys.stdout.flush()
if not times % 10 == 0:
sys.stdout.write("\r")
sys.stdout.flush()
for i in range(times % 10):
sys.stdout.write(text[i])
sys.stdout.flush()
else:
sys.stdout.write("\r")
sys.stdout.write(" ")
sys.stdout.flush()
sys.stdout.write("\n")
sys.stdout.write(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()))
sys.stdout.flush()
time.sleep(1)
times += 1
hello_world 发表于2020-02-28 12:29
不好意思,没注意。这样就行了:
import sys
import os
import time
text = list('请勿越过白色安全线')
times = 1
while True:
os.system("cls")
sys.stdout.write('新春佳节快乐\n')
sys.stdout.flush()
if not times % 10 == 0:
sys.stdout.write("\r")
sys.stdout.flush()
for i in range(times % 10):
sys.stdout.write(text[i])
sys.stdout.flush()
else:
sys.stdout.write("\r")
sys.stdout.write(" ")
sys.stdout.flush()
sys.stdout.write("\n")
sys.stdout.write(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()))
sys.stdout.flush()
time.sleep(1)
times += 1
可能是我没表达清楚,我在pycharm里运行出来是这样的。


购物车
发表新帖
立即签到







