简体   繁体   中英

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). 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:
命令

I know that it won't work straight from the IDLE, so I wonder if you can import a software like into python?

Powershell works though... 电源外壳图片

PS please don't tell me to import colorama or something. 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

EDIT: the code I put across the python programs:

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. The decision so far is that is should show program developers what their program's output, without interpretation. 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. 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. 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.

ANSI code is a broad term. You have to specify which code page you are using. For example, my Windows is in Chinese Simplified. 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. 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=...) ? 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM