Python如何让倒计时效果的在固定区域刷新
高洛峰
高洛峰 2017-04-17 15:08:26
0
1
1619
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(1)
洪涛

Sorry, it’s me again.
Let's learn more about "b" which means backspace. It will move one space to the left and delete the actual characters on that space. For details, please refer to the ascii encoding table

There are only two ways

1) Whole row coverage.

pythonimport sys, time

def interface_show(**kwargs):
    lineTmpla = ' '*5 + kwargs['title'] + kwargs['artist'] + kwargs['rate'] + " %-3s"
    print time_remain(lineTmpla, kwargs['minutes']),

def time_remain(lineTmpla, mins):
    count = 0 
    while (count < mins):
        count += 1
        n = mins - count
        time.sleep(1)
        sys.stdout.write("\r" + lineTmpla %(n),)
        sys.stdout.flush()
        if not n:
            return 'completed'

interface_show(title="倒计时demo", artist="哗嚓啊", rate="揍起来", minutes=5)

2) Partial coverage

You need to move the cursor. I only know b for now. If anyone knows the others and is willing to tell me, I would be very grateful

pythonimport sys, time

def interface_show(**kwargs):
    print ' '*5, kwargs['title'], kwargs['artist'], kwargs['rate'], time_remain(kwargs['minutes'])

def time_remain(mins):
    count = 0
    length = len(str(mins))
    sys.stdout.write(" " * length,)
    while (count < mins):
        count += 1
        n = mins - count
        time.sleep(1)
        sys.stdout.write(('\b' * length) + str(n),)
        sys.stdout.flush()
        if not n:
            return 'completed'

interface_show(title="倒计时demo", artist="哗嚓啊", rate="揍起来", minutes=5)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template