虾但不子 2023-03-24 19:35 采纳率: 100%
浏览 30
已结题

无效退格键和输出彩色字符

img


我想要使用退格键,但为什么输出一个方块?
不应该输出 plann吗?
另外一个问题

img


我想输出红色的字符,但为什么不行?

  • 写回答

1条回答 默认 最新

  • a5156520 2023-03-24 19:49
    关注

    以转义序列的形式输出彩色字符有些Windows系统好像是不支持的,在linux下支持;

    如果要在windows下输出彩色字符可以使用python标准库colorama模块,下面的代码应该是可以在Windows的cmd下显示彩色字符(但仍不可以在IDLE下显示彩色字符),并使用了\b来回退一个字符。

    测试代码如下:

    参考链接:


    
    
    # http://www.ay1.cc/article/1674826887219315330.html
    # -----------------colorama模块的一些常量---------------------------
    # Fore: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
    # Back: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
    # Style: DIM, NORMAL, BRIGHT, RESET_ALL
    #
     
    from colorama import init, Fore, Back, Style
    init(autoreset=True)
    class Colored(object):
     
      # 前景色:红色 背景色:默认
      def red(self, s):
        return Fore.RED + s + Fore.RESET
     
      # 前景色:绿色 背景色:默认
      def green(self, s):
        return Fore.GREEN + s + Fore.RESET
     
      # 前景色:黄色 背景色:默认
      def yellow(self, s):
        return Fore.YELLOW + s + Fore.RESET
     
      # 前景色:蓝色 背景色:默认
      def blue(self, s):
        return Fore.BLUE + s + Fore.RESET
     
      # 前景色:洋红色 背景色:默认
      def magenta(self, s):
        return Fore.MAGENTA + s + Fore.RESET
     
      # 前景色:青色 背景色:默认
      def cyan(self, s):
        return Fore.CYAN + s + Fore.RESET
     
      # 前景色:白色 背景色:默认
      def white(self, s):
        return Fore.WHITE + s + Fore.RESET
     
      # 前景色:黑色 背景色:默认
      def black(self, s):
        return Fore.BLACK
     
      # 前景色:白色 背景色:绿色
      def white_green(self, s):
        return Fore.WHITE + Back.GREEN + s + Fore.RESET + Back.RESET
     
    color = Colored()
    # https://blog.csdn.net/sgzqc/article/details/126354619
    print(color.red('I am red!\b!'))
    print(color.green('I am gree!\b!'))
    print(color.yellow('I am yellow!\b!'))
    print(color.blue('I am blue!\b!'))
    print(color.magenta('I am magenta!\b!'))
    print(color.cyan('I am cyan!\b!'))
    print(color.white('I am white!\b!'))
    print(color.white_green('I am white green!\b!'))
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 4月2日
  • 已采纳回答 3月25日
  • 创建了问题 3月24日

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么