简体   繁体   English

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

[英]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.我发现我可以使用GetConsoleScreenBufferInfoExSetConsoleScreenBufferInfoEx函数以编程方式更改控制台的 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.

相关问题 如何在pyqt中更改Qtablewidget的特定单元格背景颜色 - How to change Qtablewidget's specific cells background color in pyqt 如何更改 window (Python) (Tkinter) 特定部分的背景颜色 - How to change background color for a specific portion of window (Python) (Tkinter) 如何在python中设置控制台的背景颜色? - How to set background color of console in python? 如何使用opencv python将RGB颜色范围更改为红色 - how to change a range of RGB color to Red with opencv python Python 将 3 通道 rgb 彩色图像更改为 1 通道灰色的速度有多快? - How fast in Python change 3 channel rgb color image to 1 channel gray? 在Windows上的GTK中,如何更改整个应用程序的背景色? - In GTK on Windows, how do I change the background color of the whole app? 如何将 rgb 颜色值传递给 python 的 matplotlib eventplot? - How to pass rgb color values to python's matplotlib eventplot? 在图像文件中将特定的 RGB 颜色像素更改为另一种颜色 - Change specific RGB color pixels to another color, in image file 如何使用 python 3.9 更改屏幕上像素的颜色 - How to change color of pixel on screen with python 3.9 如何在Tkinter中更改框的背景色(Python) - How to change the background color of the box in tkinter (python)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM