python代码字体大小_Python以编程方式更改控制台字体大小

我想首先指出:

我把你的密码改了一点。在

代码.py:#!/usr/bin/env python

import sys

from ctypes import POINTER, WinDLL, Structure, sizeof, byref

from ctypes.wintypes import BOOL, SHORT, WCHAR, UINT, ULONG, DWORD, HANDLE

LF_FACESIZE = 32

STD_OUTPUT_HANDLE = -11

class COORD(Structure):

_fields_ = [

("X", SHORT),

("Y", SHORT),

]

class CONSOLE_FONT_INFOEX(Structure):

_fields_ = [

("cbSize", ULONG),

("nFont", DWORD),

("dwFontSize", COORD),

("FontFamily", UINT),

("FontWeight", UINT),

("FaceName", WCHAR * LF_FACESIZE)

]

kernel32_dll = WinDLL("kernel32.dll")

get_last_error_func = kernel32_dll.GetLastError

get_last_error_func.argtypes = []

get_last_error_func.restype = DWORD

get_std_handle_func = kernel32_dll.GetStdHandle

get_std_handle_func.argtypes = [DWORD]

get_std_handle_func.restype = HANDLE

get_current_console_font_ex_func = kernel32_dll.GetCurrentConsoleFontEx

get_current_console_font_ex_func.argtypes = [HANDLE, BOOL, POINTER(CONSOLE_FONT_INFOEX)]

get_current_console_font_ex_func.restype = BOOL

set_current_console_font_ex_func = kernel32_dll.SetCurrentConsoleFontEx

set_current_console_font_ex_func.argtypes = [HANDLE, BOOL, POINTER(CONSOLE_FONT_INFOEX)]

set_current_console_font_ex_func.restype = BOOL

def main():

# Get stdout handle

stdout = get_std_handle_func(STD_OUTPUT_HANDLE)

if not stdout:

print("{:s} error: {:d}".format(get_std_handle_func.__name__, get_last_error_func()))

return

# Get current font characteristics

font = CONSOLE_FONT_INFOEX()

font.cbSize = sizeof(CONSOLE_FONT_INFOEX)

res = get_current_console_font_ex_func(stdout, False, byref(font))

if not res:

print("{:s} error: {:d}".format(get_current_console_font_ex_func.__name__, get_last_error_func()))

return

# Display font information

print("Console information for {:}".format(font))

for field_name, _ in font._fields_:

field_data = getattr(font, field_name)

if field_name == "dwFontSize":

print(" {:s}: {{X: {:d}, Y: {:d}}}".format(field_name, field_data.X, field_data.Y))

else:

print(" {:s}: {:}".format(field_name, field_data))

while 1:

try:

height = int(input("\nEnter font height (invalid to exit): "))

except:

break

# Alter font height

font.dwFontSize.X = 10 # Changing X has no effect (at least on my machine)

font.dwFontSize.Y = height

# Apply changes

res = set_current_console_font_ex_func(stdout, False, byref(font))

if not res:

print("{:s} error: {:d}".format(set_current_console_font_ex_func.__name__, get_last_error_func()))

return

print("OMG! The window changed :)")

# Get current font characteristics again and display font size

res = get_current_console_font_ex_func(stdout, False, byref(font))

if not res:

print("{:s} error: {:d}".format(get_current_console_font_ex_func.__name__, get_last_error_func()))

return

print("\nNew sizes X: {:d}, Y: {:d}".format(font.dwFontSize.X, font.dwFontSize.Y))

if __name__ == "__main__":

print("Python {:s} on {:s}\n".format(sys.version, sys.platform))

main()

注意事项:ctypes允许类似于C的低级访问(只有语法是Python)

使用ctypes.wintypes常量(引用标准的ctypes类型)(给代码一种类似于Win的风格)

它非常(几乎痛苦地)长(也因为我添加了正确的错误处理)

另一种选择,如[SO]: Change console font in Windows(您从中复制代码)的一个答案中建议的,将是安装一个3rd方模块(例如[GitHub]: mhammond/pywin32 - Python for Windows (pywin32) Extensions,它是一个Python的包装器,覆盖了WINAPIs),这将需要更少的代码来编写,因为Python和C之间的桥接已经实现,而且上述功能可能只需几行代码就可以完成

正如我在代码中所评论的,设置COORD.X似乎被忽略了。但是当设置COORD.Y(设置为接近COORD.Y // 2的值-可能是为了保持纵横比)时,它会自动设置。在我的机器上(Win 10x64),默认值是16。你可能想在最后把它放回去,以避免让控制台处于“挑战”状态(显然,Win调整cmd窗口大小,与字体大小(某种程度上)同步):

H7VA6.jpg

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值