【问题标题】:How to programmatically change the background color of Python's whole console screen to a specific RGB color?如何以编程方式将 Python 整个控制台屏幕的背景颜色更改为特定的 RGB 颜色?
【发布时间】:2020-11-21 19:27:51
【问题描述】:

我希望能够将 Python 控制台 (python.exe) 的背景颜色更改为任何 RGB 颜色。

我知道我可以使用颜色命令(documentation link 1documentation link 2)从 16 种可用颜色中选择背景和前景(字体)颜色:

import os

os.system('color 8f')

我还知道我可以通过右键单击控制台的顶部栏然后单击属性并转到颜色选项卡来手动更改这 16 种颜色的 RGB 值。

我想知道是否有编程解决方案,就像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')

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

【问题讨论】:

  • 你问如何改变IDLE的背景颜色?
  • @wwii 不是 IDLE shell(来自C:\Users\Username\AppData\Local\Programs\Python\Python38-32\pythonw.exe "C:\Users\Username\AppData\Local\Programs\Python\Python38-32\Lib\idlelib\idle.pyw")。 python.exe 一(C:\Users\Username\AppData\Local\Programs\Python\Python38-32\python.exe)。

标签: python cmd colors background windows-10


【解决方案1】:

我发现我可以使用 GetConsoleScreenBufferInfoExSetConsoleScreenBufferInfoEx 函数以编程方式更改控制台 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 之后)。

【讨论】:

    猜你喜欢
    • 2011-09-21
    • 2021-09-27
    • 1970-01-01
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多