Python colorama not working with input?

拥有回忆 提交于 2019-12-07 03:30:17

问题


Finally got colorama working today, and it works excellent when printing strings, but I got the common error everyone seems to get when I attempted to use colorama with input.

Here's my code:

launch = input(Fore.GREEN + "Launch attack?(Y/N): ")

Screenshot of output:


回答1:


On my system, input() works with colors if you add

import sphinx.quickstart

to your module.

So here is the full code.

from colorama import Fore
import colorama
import sphinx.quickstart
colorama.init()
launch = input(Fore.GREEN + "Launch attack? (Y/N): ")

(This leads to two questions:

  1. Why does it not work in the first place?
  2. What is the actual reason? – Someone might like to dive into the sphinx source code.)

N.B. if you run python via winpty from Git Bash, set convert.

colorama.init(convert=True)

Otherwise, you do not get color with the current versions.




回答2:


I had this same issue (Python 3.5.4) and, just in case it is not too obvious for somebody else looking at this, you can always rely on the workaround of combining print / input calls where you previously had just an input call:

print(Fore.GREEN + "Launch attack?(Y/N): ", end='')
launch = input()

This should produce the exact same output as in your question, with no extra blank lines and with the code coloring working without the need of importing anything else.

The (small?) disadvantage is that you you will end up with two lines of code where you previously had just one.



来源:https://stackoverflow.com/questions/32872612/python-colorama-not-working-with-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!