完成启蒙进度条仍在屏幕上

问题描述 投票:0回答:2

我已经使用Python中的enlight进度条包完成了多个计数器的实现。

问题: 在以下程序中,

'A - first bar'
仍在屏幕上,而
'B - first bar'
正在计数。

要求: 一旦

run_first_loop
完成执行,我希望进度条从屏幕上删除。 因此,最后,当
'B - first bar'
正在计数时,不应该有任何其他进度条。

提前致谢!

import time
import enlighten


def run_first_loop(manager):
    ticks = manager.counter(total=100, desc='A - first bar', unit='ticks', leave=False)
    tocks = manager.counter(total=20, desc='A - second bar', unit='tocks', leave=False)

    for num in range(100):
        time.sleep(0.1)  # Simulate work
        print(num)
        ticks.update()
        if not num % 5:
            tocks.update()
    ticks.close()
    tocks.close()


def run_sec_loop(manager):
    new_ticks = manager.counter(total=100, desc='B - first bar', unit='ticks', leave=False)

    for num in range(100):
        time.sleep(0.1)  # Simulate work
        print(num)
        new_ticks.update()
    new_ticks.close()


manager = enlighten.get_manager()

run_first_loop(manager)
time.sleep(2) # I'm expecting "A-first bar" should be removed from the screen. But, it's not.
run_sec_loop(manager) # at this point, the previous bar is also there on the screen, which is a little annoying.
manager.stop()
python progress-bar
2个回答
2
投票

您可以将

clear=True
放入
.close()
方法以在关闭时清除栏:

import time
import enlighten


def run_first_loop(manager):
    ticks = manager.counter(total=100, desc="A - first bar", unit="ticks", leave=False)
    tocks = manager.counter(total=20, desc="A - second bar", unit="tocks", leave=False)

    for num in range(100):
        time.sleep(0.1)  # Simulate work
        print(num)
        ticks.update()
        if not num % 5:
            tocks.update()

    ticks.close(clear=True)  # <-- put clear=True here
    tocks.close(clear=True)  # <-- put clear=True here


def run_sec_loop(manager):
    new_ticks = manager.counter(
        total=100, desc="B - first bar", unit="ticks", leave=False
    )

    for num in range(100):
        time.sleep(0.1)  # Simulate work
        print(num)
        new_ticks.update()
    new_ticks.close()


manager = enlighten.get_manager()

run_first_loop(manager)
time.sleep(
    2
)  # I'm expecting "A-first bar" should be removed from the screen. But, it's not.
run_sec_loop(
    manager
)  # at this point, the previous bar is also there on the screen, which is a little annoying.
manager.stop()

0
投票

leave
的争论可能有点令人困惑。当休假为
False
时,经理会在进度条关闭时忘记该进度条,但不会覆盖它。仅当另一个进度条取代它的位置或发生调整大小事件时才会发生这种情况。这就是为什么如果您想在关闭时清除该栏,则需要使用
close(clear=True)

© www.soinside.com 2019 - 2024. All rights reserved.