I'm trying to combine the getch and progressbar Python modules on Linux, but I cannot get it to work. I want to use getch to listen for keyboard input to interrupt the progress bar, but when I insert the getch statement, the progress bar refuses to update automatically, only updating when I press a button on the keyboard.
The code I'm currently using is below. I'm using ProgressBar2 and the getch module, but I've tried using tqdm and my own getch method with no avail.
bar = progressbar.ProgressBar()
for i in range(101):
sleep(0.01)
bar.update(i)
ch = getch.getch()
When using my own implementation of getch, I've narrowed down the problem to be with the 'sys.stdin.read(1)' line in the following code.
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno(), termios.TCSADRAIN)
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
On Windows, using the msvcrt module, I have no problems whatsoever.