简体   繁体   English

ANSI 转义码在 IDLE 上不起作用……(python)

[英]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 but the beastly symbol still appeared, this time as a boxed question mark:之前我看过几篇帖子抱怨 IDLE 不支持这些转义序列,因为它不是终端,所以我尝试直接从执行此操作,但野兽符号仍然出现,这次是一个带框的问号:
命令

I know that it won't work straight from the IDLE, so I wonder if you can import a software like into python?我知道它不会直接从 IDLE 工作,所以我想知道你是否可以将像这样的软件导入 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.然后就可以读写简体中文编码的文本文件了。

Second Question:第二个问题:

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 statement.它应该可以工作,但我想知道我如何(如果可能的话)使用一些程序自动执行此操作,该程序生成包含这些字符的文件并使用之前的语句显示它们。

Cheers!干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在python中处理终端颜色代码(ANSI颜色转义代码) - Handling terminal colour codes ( ANSI colour escape codes ) in python 如何检测控制台是否支持Python中的ANSI转义码? - How to detect if the console does support ANSI escape codes in Python? Python使用ANSI转义码在终端上不再可见的行上打印 - Python print on lines that are not visible anymore on the terminal with ANSI escape codes 使用 ANSI 转义码 (Windows) 在 python 3.8 中更改控制台打印颜色 - Change console print color in python 3.8 with ANSI escape codes (Windows) ANSI图形代码和Python - ANSI graphic codes and Python Python 和 ANSI 转义字符 - Python and ANSI escape characters 如何使用ansi转义码为python中的特定字符单元着色,其中字符单元格位置由变量确定 - How to colour a specific character cell in python using ansi escape codes where the character cell location is determined by variables Python:如何使 ANSI 转义码也能在 Windows 中工作? - Python: How can I make the ANSI escape codes to work also in Windows? 如何让 python 解释从文本文件读取的字符串中 colors 的 ANSI 转义码 - How do I get python to interpret the ANSI escape codes for colors in a string read from a text file 如何利用 input() 命令在 Python 中包含 ANSI 转义颜色代码? - How do I utilize the input() command to include ANSI escape color codes in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM