-
-
Notifications
You must be signed in to change notification settings - Fork 102
Closed
Description
Description
Chinese characters lead to line breaks or print repeatedly, because of the characters' width calculated error.
Code
-
use
FormatCustomText:import progressbar import time total = 12 current = 0 format_custom_text = progressbar.FormatCustomText( '%(progress)6s: ', dict( progress='进度' ), ) bar = progressbar.ProgressBar( widgets=[ format_custom_text, progressbar.Bar(), ' ', progressbar.Counter(), ' / {0}'.format(total) ], max_value=total, redirect_stdout=True ) bar.start() bar.update(current) while current < total: current += 1 bar.update(current) time.sleep(0.5) bar.finish()
-
use raw text:
import progressbar import time total = 12 current = 0 tip = '进度: ' bar = progressbar.ProgressBar( widgets=[ tip, progressbar.Bar(), ' ', progressbar.Counter(), ' / {0}'.format(total) ], max_value=total, redirect_stdout=True ) bar.start() bar.update(current) while current < total: current += 1 bar.update(current) time.sleep(0.5) bar.finish()
-
fill length of str with 6(a chinese character's width is 2) by using
\0import progressbar import time total = 12 current = 0 tip = '进\0度\0: ' bar = progressbar.ProgressBar( widgets=[ tip, progressbar.Bar(), ' ', progressbar.Counter(), ' / {0}'.format(total) ], max_value=total, redirect_stdout=True ) bar.start() bar.update(current) while current < total: current += 1 bar.update(current) time.sleep(0.5) bar.finish()
ps: 3 is the effect i expected.
Versions
- Python version: 3.5.2
- Python distribution/environment: CPython
- Operating System: Ubuntu 16.04 LTS
- Package version: 3.33.2
maybe helpful
wcwidth Python library that measures the width of unicode strings rendered to a terminal
Metadata
Metadata
Assignees
Labels
No labels


