'How to change console font for windows 11 and windows 10 in python

I am currently making an ascii-based video game, I want to change the command prompt font to the 'Terminal' font because the original font is ugly.

Here is the code that changes the font:

LF_FACESIZE = (32)
STD_OUTPUT_HANDLE = (-11)


class COORD(ctypes.Structure):
    _fields_ = [("X", (ctypes.c_short)), ("Y", (ctypes.c_short))]


class CONSOLE_FONT_INFOEX(ctypes.Structure):
    _fields_ = [("cbSize", (ctypes.c_ulong)),
                ("nFont", (ctypes.c_ulong)),
                ("dwFontSize", (COORD)),
                ("FontFamily", (ctypes.c_uint)),
                ("FontWeight", (ctypes.c_uint)),
                ("FaceName", ((ctypes.c_wchar) * (LF_FACESIZE)))]


font = (CONSOLE_FONT_INFOEX())
font.cbSize = (ctypes.sizeof(CONSOLE_FONT_INFOEX))
font.nFont = 12
if user32.GetSystemMetrics(0) <= 1440:
    font.dwFontSize.X = 8
    font.dwFontSize.Y = 12
else:
    font.dwFontSize.X = 17
    font.dwFontSize.Y = 30
font.FontFamily = 80
font.FontWeight = 400
font.FaceName = "Terminal"

handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
ctypes.windll.kernel32.SetCurrentConsoleFontEx(
    handle, ctypes.c_long(False), ctypes.pointer(font))

This code works for windows 10. But when my friend who uses windows 11 ran the program, the font did not change.

Also this code uses ctypes.



Solution 1:[1]

Should work on windows 11 as I just tested it, I'm just looking on how to make it a square font so I can do graphics, anyway here is my module, hope it helps you!

import ctypes
def font(size):
    LF_FACESIZE = 32
    STD_OUTPUT_HANDLE = -11

    class COORD(ctypes.Structure):
        _fields_ = [("X", ctypes.c_short), ("Y", ctypes.c_short)]

    class CONSOLE_FONT_INFOEX(ctypes.Structure):
        _fields_ = [("cbSize", ctypes.c_ulong),
                    ("nFont", ctypes.c_ulong),
                    ("dwFontSize", COORD),
                    ("FontFamily", ctypes.c_uint),
                    ("FontWeight", ctypes.c_uint),
                    ("FaceName", ctypes.c_wchar * LF_FACESIZE)]

    font = CONSOLE_FONT_INFOEX()
    font.cbSize = ctypes.sizeof(CONSOLE_FONT_INFOEX)
    font.nFont = 12
    font.dwFontSize.X = size[0]
    font.dwFontSize.Y = size[1]
    font.FontFamily = 54
    font.FontWeight = 400
    font.FaceName = "Proggy Square"

    handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
    ctypes.windll.kernel32.SetCurrentConsoleFontEx(
        handle, ctypes.c_long(False), ctypes.pointer(font))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RedstoneHair