进度条出现问题怎么解决
使用alive_progress创建进度条制作一个生成鬼畜字符的程序时,进度条经常会在运行一段时间后重复输出大量进度条,并且产生类似将动画的每一帧分开一样的效果。一般在进度条运行到74%左右时会出现这种情况。
import sys
import random
import pyperclip
from alive_progress import alive_bar
text=open("ready.txt").read().split("\n")
table='`1234567890-=qwertyuiop[]\\asdfghjkl;\'zxcvbnm,./~!@#$%^&*()_+QWERTYUIOP\
{}|ASDFGHJKL:"ZXCVBNM<>?'
start="ctrl+c"
end="ctrl+v"
temp=start
N=50000;M=12
for i in range(M):
with alive_bar(N) as bar:
for j in range(N):
tmp=random.choice(text)
temp+=chr(eval("0x"+tmp))
bar()
temp+=random.choice(list(table))
temp+=end
pyperclip.copy(temp)
其中ready.txt文件保存了鬼畜字符的Unicode编码,版本为python3.11.4
输出:
问题点: 进度条重复出现
分析思路:其中,bar()默认走一个进度,如果写bar(num),则走num个进度.
解决办法:设参数refresh_secs=1 每秒刷新一次进度.
代码修改为:
import sys
import random
import pyperclip
from alive_progress import alive_bar
text=open("ready.txt").read().split("\n")
table='`1234567890-=qwertyuiop[]\\asdfghjkl;\'zxcvbnm,./~!@#$%^&*()_+QWERTYUIOP\
{}|ASDFGHJKL:"ZXCVBNM<>?'
start="ctrl+c"
end="ctrl+v"
temp=start
N=50000
M=12
for i in range(M):
with alive_bar(N, force_tty=True, refresh_secs=1) as bar:
for j in range(N):
tmp=random.choice(text)
temp+=chr(eval("0x"+tmp))
bar()
temp+=random.choice(list(table))
temp+=end
pyperclip.copy(temp)