2

我正在尝试在 Python 中制作一个虚假的引导序列,其中一个元素是进度条。进度条应保持在一行,而不是重复打印多行。一段时间后,我创建了以下代码:

from time import sleep
import random

def progressbar(title, length):
    add = 0
    strlength = len(title) + 3
    barlength = '['
    for i in range(length - 1):
        barlength = barlength + '.'
    barlength = barlength + ']'
    progress = title + ': ' + barlength
    for i in range(length):
        print(progress, end='\r')
        append = add + strlength
        progress = list(progress)
        progress[append] = '#'
        progress = str(''.join(progress))
        sleep(random.uniform(0.2, 1.0))
        add = add + 1

print("Username: TestUserABC, Password: password1")
sleep(1)
print('---[STARTING BOOT SEQUENCE]---')
print(':-Welcome to GMOS (Generic Movie Operating System)-:')
progressbar('Checking Filesystem', 10)

但是,它不会覆盖一行,而是输出:

Username: TestUserABC, Password: password1
---[STARTING BOOT SEQUENCE]---
:-Welcome to GMOS (Generic Movie Operating System)-:
Checking Filesystem: [.........]Checking Filesystem: [#........]Checking Filesystem: 
[##.......]Checking Filesystem: [###......]Checking Filesystem: [####.....]Checking Filesystem: 
[#####....]Checking Filesystem: [######...]Checking Filesystem: [#######..]Checking Filesystem: 
[########.]Checking Filesystem: [#########]

我将不胜感激,因为我不知道是什么原因造成的。为了清楚起见,我在 Windows 10 上,以防万一字符串......操作系统之间的事情不同,我怀疑这种事情。(有人能告诉我像 \n 或 \r 这样的东西叫什么吗?)

4

2 回答 2

1

这可以通过使用“sys.stdout.flush()”来完成。您可以在此处阅读有关此功能的更多信息:https ://www.geeksforgeeks.org/python-sys-stdout-flush/

您的代码的使用示例:

from time import sleep
import random
import sys

def progressbar(title, length):
    add = 0
    strlength = len(title) + 3
    barlength = '['
    for i in range(length - 1):
        barlength = barlength + '.'
    barlength = barlength + ']'
    progress = title + ': ' + barlength
    for i in range(length):
        print(progress, end='\r')
        sys.stdout.flush()
        append = add + strlength
        progress = list(progress)
        progress[append] = '#'
        progress = str(''.join(progress))
        sleep(random.uniform(0.2, 1.0))
        add = add + 1

print("Username: TestUserABC, Password: password1")
sleep(1)
print('---[STARTING BOOT SEQUENCE]---')
print(':-Welcome to GMOS (Generic Movie Operating System)-:')
progressbar('Checking Filesystem', 10)
print('')

添加:

  • 导入系统
  • sys.stdout.flush()
  • 打印('')
于 2020-12-21T22:12:33.207 回答
0

这个(简化的)代码对我有用:

from time import sleep
import random

def progressbar(title, length):
    progress = ['.' for _ in range(10)]
    
    print(f"\r{title}: [{''.join(progress)}]", end='', flush=True)
    for i in range(10):
        print(f"\r{title}: [{''.join(progress)}]", end='', flush=True)
        sleep(random.uniform(0.2, 1.0))
        progress[i] = '#'
    print(f"\r{title}: [{''.join(progress)}]", flush=True)

print("Username: TestUserABC, Password: password1")
sleep(1)
print('---[STARTING BOOT SEQUENCE]---')
print(':-Welcome to GMOS (Generic Movie Operating System)-:')
progressbar('Checking Filesystem', 10)

输出:

Username: TestUserABC, Password: password1
---[STARTING BOOT SEQUENCE]---
:-Welcome to GMOS (Generic Movie Operating System)-:

Checking Filesystem: [..........] # up to Checking Filesystem: [##########]

另存为 'test.py' ,使用控制台并通过python.exe test.py(python.exe in %PATH%)调用它。请参阅如何刷新打印功能的输出?.

您可以使用curses (*nix)UniCurses (Win*)模块对您的输出产生更大的影响。

于 2020-12-21T22:09:39.297 回答