[英]How to programmatically change the background color of Python's whole console screen to a specific RGB color?
I want to be able to change the background color of Python's console (python.exe) to any RGB color.我希望能够将 Python 控制台(python.exe)的背景颜色更改为任何 RGB 颜色。
I know that I can use the color command ( documentation link 1 and documentation link 2 ) to pick the background and foreground (font) colors from 16 available colors:我知道我可以使用颜色命令(文档链接 1和文档链接 2 )从 16 个可用的 colors 中选择背景和前景(字体)colors:
import os
os.system('color 8f')
I also know that I can manually change the RGB values of those 16 colors by right-clicking the top bar of the console and then clicking in properties and going to the colors' tab.我也知道我可以通过右键单击控制台的顶部栏然后单击属性并转到颜色选项卡来手动更改这 16 个 colors 的 RGB 值。
I would like to know if there's a programmatic solution though, just as there is for changing the font color to an RGB value :我想知道是否有编程解决方案,就像将字体颜色更改为 RGB 值一样:
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')
I'm asking for the background color of the whole console screen, not the background color of the text .我要的是整个控制台屏幕的背景颜色,而不是text 的背景颜色。 I also don't want to install some different console or a module that somehow does it without understanding how.
我也不想安装一些不同的控制台或模块,在不了解如何的情况下以某种方式做到这一点。
I found out that I can use the GetConsoleScreenBufferInfoEx and SetConsoleScreenBufferInfoEx functions to programmatically change the values of any of the 16 colors of the console.我发现我可以使用GetConsoleScreenBufferInfoEx和SetConsoleScreenBufferInfoEx函数以编程方式更改控制台的 16 个 colors 中的任何一个的值。
The code below is a combination of the code found in the question and answers:下面的代码是问题和答案中找到的代码的组合:
Why does the console window shrink when using GetConsoleScreenBufferInfoEx in Windows?为什么在 Windows 中使用 GetConsoleScreenBufferInfoEx 时控制台 window 会缩小?
Executable called via subprocess.check_output prints on console but result is not returned通过 subprocess.check_output 调用的可执行文件在控制台上打印,但不返回结果
Change entire console background color (Win32 C++)更改整个控制台背景颜色 (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))
The first color (black by default) is the one being changed since it's the default one being used out of the 16 as background color.第一种颜色(默认为黑色)是正在更改的颜色,因为它是 16 种颜色中的默认颜色作为背景颜色。
If you need to update the screen color for example at the start of a program you can just do os.system('cls')
(after SetConsoleScreenBufferInfoEx
).如果您需要更新屏幕颜色,例如在程序开始时,您可以执行
os.system('cls')
(在SetConsoleScreenBufferInfoEx
之后)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.