[英]ANSI escape codes not working on IDLE… (python)
I am making a program involving ANSI escape codes , and they were all working fine on replit until I switched back to IDLE (3.9 on both).我正在制作一个涉及ANSI 转义码的程序,在我切换回 IDLE 之前,它们在repli上都运行良好(两者都为 3.9)。 But it doesn't work:
但它不起作用:
it should have looked like this:它应该看起来像这样:
I have seen several posts before that complain that the IDLE doesn't support these escape sequences because it isn't a terminal, so I tried to do it directly from the cmd but the beastly symbol still appeared, this time as a boxed question mark:之前我看过几篇帖子抱怨 IDLE 不支持这些转义序列,因为它不是终端,所以我尝试直接从cmd执行此操作,但野兽符号仍然出现,这次是一个带框的问号:
I know that it won't work straight from the IDLE, so I wonder if you can import a software like mintty into python?我知道它不会直接从 IDLE 工作,所以我想知道你是否可以将像mintty这样的软件导入 python 中?
Powershell works though... Powershell 工作虽然...
PS please don't tell me to import colorama or something. PS请不要告诉我导入colorama或其他东西。 I really want this to be the way.
我真的希望这样。 I also don't have immediate access to iPython (even though I would like to) so it's not really an option for me..: unless I have to :D
我也无法立即访问 iPython(即使我想这样做),所以这对我来说不是一个真正的选择..:除非我必须:D
EDIT: the code I put across the python programs:编辑:我在 python 程序中放置的代码:
import sys, os
os.system("")
CSI = f"{chr(0x1B)}["
print(f"""{CSI}3m{CSI}1m{CSI}4m{CSI}31m
look at this""")
sys.stdout.flush()
# I put the sys.stdout.flush() and os.system("") to try and fix the problem...
The IDLE shell is not a terminal emulator and not intended to be production environment. IDLE shell 不是终端仿真器,也不是生产环境。 The decision so far is that is should show program developers what their program's output, without interpretation.
到目前为止的决定是应该向程序开发人员展示他们的程序的 output 是什么,无需解释。 This may change in the future but no final decision yet.
这可能会在未来发生变化,但还没有最终决定。
If you are on Windows, I believe its console can be put into ANSI mode, but Python does not do that for you.如果您使用的是 Windows,我相信它的控制台可以进入 ANSI 模式,但 Python 不会为您这样做。 I don't know if program code can do so.
我不知道程序代码是否可以这样做。
As near as I can tell, there is not wrapper program that turns mintty into an importable python module.据我所知,没有包装程序可以将 mintty 变成可导入的 python 模块。 It would not make much sense.
这没有多大意义。 Rather, you would want to open mintty or a mintty-based terminal-emulator, such as git bash, and open python within that terminal instead of CommandPrompt.
相反,您需要打开 mintty 或基于 mintty 的终端仿真器,例如 git bash,并在该终端中打开 python 而不是 CommandPrompt。
ANSI code is a broad term. ANSI 代码是一个广义术语。 You have to specify which code page you are using.
您必须指定您使用的代码页。 For example, my Windows is in Chinese Simplified.
比如我的Windows是简体中文。 Therefore if I want to escape from UTF-8 default in Python, I would put
# coding: cp936
on the first or second line of a script.因此,如果我想摆脱 Python 中的 UTF-8 默认值,我会将
# coding: cp936
放在脚本的第一行或第二行。 Then it can read and write text files with the simplified Chinese coding.然后就可以读写简体中文编码的文本文件了。
Could I make a red/green/etc.我可以做一个红色/绿色/等吗? font for every character and put it as
print('...', file=...)
?每个字符的字体并将其作为
print('...', file=...)
? It should be possible because colored emojis exist.这应该是可能的,因为存在彩色表情符号。
It should work, but I would like to know how I could (if it's possible) automate this with some program that makes a file containing those characters and displays them with the previous print statement.它应该可以工作,但我想知道我如何(如果可能的话)使用一些程序自动执行此操作,该程序生成包含这些字符的文件并使用之前的打印语句显示它们。
Cheers!干杯!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.