已有183人关注
两行文字分别动态显示效果出现错误
发表在Python图书答疑 2020-02-22 《Python编程锦囊》第1章 核心基础应用 10页-11页
是否精华
版块置顶:
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()

请问下代码出现了什么问题,导致两行文字的动态效果串行了。

分享到:
精彩评论 8
hello_world
学分:154 LV4
2020-02-22
沙发

虽然你开启了两个进程,但这两个进程使用的是同一个光标,使用会串行

公子羽爱猫
学分:196 LV4
2020-02-24
板凳

hello_world 发表于2020-02-22 12:20

虽然你开启了两个进程,但这两个进程使用的是同一个光标,使用会串行

那请问下应该怎么修改呢?

hello_world
学分:154 LV4
2020-02-26
地板

公子羽爱猫 发表于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中正常运行

公子羽爱猫
学分:196 LV4
2020-02-27
4L

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中正常运行

请勿越过白色安全线应该在第二行,而时间是第三行,运行了一下,显示出的效果不对。题目要求的效果如图:

image003.jpg


hello_world
学分:154 LV4
2020-02-28
5L

公子羽爱猫 发表于2020-02-27 03:11

请勿越过白色安全线应该在第二行,而时间是第三行,运行了一下,显示出的效果不对。题目要求的效果如图:

image003.jpg


不好意思,没注意。这样就行了:

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

公子羽爱猫
学分:196 LV4
2020-02-28
6L

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里运行出来是这样的。

1.jpg


hello_world
学分:154 LV4
2020-03-03
7L

公子羽爱猫 发表于2020-02-28 20:16

可能是我没表达清楚,我在pycharm里运行出来是这样的。

1.jpg


不好意思,因为语句os.system是在CMD中清屏的作用,使用PyCharm运行时无法清屏!

公子羽爱猫
学分:196 LV4
2020-03-03
8L

hello_world 发表于2020-03-03 16:46

不好意思,因为语句os.system是在CMD中清屏的作用,使用PyCharm运行时无法清屏!

哦哦,在cmd中确实可以正常运行。还有个问题要请教一下,cmd中无法按照图片一样显示不同文字,但是pycharm中又无法清屏,那要怎么样才能做到图片效果呢?

首页上一页 1 下一页尾页 8 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照