代码之家  ›  专栏  ›  技术社区  ›  user7393973

如何通过编程将Python整个控制台屏幕的背景颜色更改为特定的RGB颜色?

  •  0
  • user7393973  · 技术社区  · 4 年前

    我知道我可以使用颜色命令( documentation link 1 documentation link 2 )要从16种可用颜色中选择背景和前景(字体)颜色,请执行以下操作:

    import os
    
    os.system('color 8f')
    

    image of the console with the background colored from running the code above

    我想知道是否有一个程序化的解决方案,就像对 changing the font color to an RGB value :

    import os
    
    os.system('')
    
    def rgb(red, green, blue):
      return f'\x1b[38;2;{red};{green};{blue}m'
    
    red_color = rgb(255, 0, 0)
    green_color = rgb(0, 255, 0)
    blue_color = rgb(0, 0, 255)
    
    print(f'{red_color}red {green_color}green {blue_color}blue')
    

    image of the console with the font colored from running the code above

    我要的是整个控制台屏幕的背景色,而不是背景色 background color of the text . 我也不想安装一些不同的控制台或模块,在不了解如何操作的情况下以某种方式进行安装。

    0 回复  |  直到 4 年前
        1
  •  1
  •   user7393973    4 年前

    我发现我可以使用 GetConsoleScreenBufferInfoEx SetConsoleScreenBufferInfoEx 函数以编程方式更改控制台16种颜色中任意一种颜色的值。

    下面的代码是问答中的代码组合:

    Why does the console window shrink when using GetConsoleScreenBufferInfoEx in Windows?

    Executable called via subprocess.check_output prints on console but result is not returned

    Change entire console background color (Win32 C++)

    import ctypes
    from ctypes import wintypes
    import os
    import sys
    import time
    
    class COORD(ctypes.Structure):
      _fields_ = (('X', wintypes.SHORT), ('Y', wintypes.SHORT))
    
    class CONSOLE_SCREEN_BUFFER_INFOEX(ctypes.Structure):
      _fields_ = (
        ('cbSize', wintypes.ULONG),
        ('dwSize', COORD),
        ('dwCursorPosition', COORD),
        ('wAttributes', wintypes.WORD),
        ('srWindow', wintypes.SMALL_RECT),
        ('dwMaximumWindowSize', COORD),
        ('wPopupAttributes', wintypes.WORD),
        ('bFullscreenSupported', wintypes.BOOL),
        ('ColorTable', wintypes.DWORD * 16))
      def __init__(self, *args, **kwds):
        super(CONSOLE_SCREEN_BUFFER_INFOEX, self).__init__(*args, **kwds)
        self.cbSize = ctypes.sizeof(self)
    
    def rgb_values_to_integer_color(red, green, blue):
      integer_color = red + (green * 256) + (blue * 256 * 256)
      return integer_color
    
    STD_OUTPUT_HANDLE = -11
    console_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
    console_screen_information = CONSOLE_SCREEN_BUFFER_INFOEX()
    
    # get the original color to later set it back
    ctypes.windll.kernel32.GetConsoleScreenBufferInfoEx(console_handle, ctypes.byref(console_screen_information))
    original_color = console_screen_information.ColorTable[0]
    
    # prevent the console screen's height from shrinking
    console_screen_information.srWindow.Bottom += 1
    
    # set the new rgb color
    console_screen_information.ColorTable[0] = rgb_values_to_integer_color(red=84, green=170, blue=255)
    ctypes.windll.kernel32.SetConsoleScreenBufferInfoEx(console_handle, ctypes.byref(console_screen_information))
    
    # wait 3 seconds
    time.sleep(3)
    
    # change back to the original color
    console_screen_information.ColorTable[0] = original_color
    ctypes.windll.kernel32.SetConsoleScreenBufferInfoEx(console_handle, ctypes.byref(console_screen_information))
    

    第一种颜色(默认为黑色)是正在更改的颜色,因为它是16种颜色中用作背景色的默认颜色。

    os.system('cls') (之后 SetConsoleScreenBufferInfoEx ).