Colorama 不會輸出給定的顏色

Colorama 不會輸出給定的顏色

我一直在使用 colorama,並且在網路上看到了一些答案,說 colorama 僅適用於終端。我已經使用啟動器作為 exe 打開了我的 python 文件,它總是工作得很好。然而這一次,情況有所改變。當我在 VSCode 中運行此程式碼片段時:

from colorama import Fore, Style

green = Fore.GREEN
print(f'{green}This is a test')

一切運作良好,我的輸出是綠色的。但是當我使用 python 啟動器運行我的文件時,我得到了這個:[32mThis is a test

我知道 colorama 仍然可以與啟動器一起使用,因為我幾天前已經使用過它。通過啟動器,我為我的無知道歉,你知道當你右鍵單擊一個文件並且它說用“Python”打開時如何。我稱之為啟動器。 StackOverflow 的有人說在這裡問我關於顏色代碼的問題。任何見解都將不勝感激

答案1

這在以下情況下不起作用空閒外殼當您透過整合開發和學習環境(IDLE)啟動它時。

在 Windows 上,請改用 PowerShell,並將以下程式碼儲存在桌面資料夾中new-2.py

from colorama import init, Fore, Style
init()

# by Colorama’s constant shorthand for ANSI escape sequences:
# -----------------------------------------------------------
from colorama import Fore, Back, Style
print('\033[31m' + 'some red text')
print('\033[39m') # and reset to default color

# by manually printing ANSI sequences from your own code:
# -------------------------------------------------------
print(Fore.RED + 'some red text')
print(Back.GREEN + 'and with a green background')
print(Style.DIM + 'and in dim text')
print(Style.RESET_ALL)
print('back to normal now')

# by using your code sample
# -------------------------
green = Fore.GREEN
print(f'{green}This is a test')

這導致以下輸出:

在此輸入影像描述

相關內容