python中字体⼤⼩怎么修改代码_Python以编程⽅式更改控制
台字体⼤⼩
我发现下⾯的代码应该以编程⽅式更改控制台字体⼤⼩.我在Windows 10上.
但是,⽆论我调整什么值,我似乎都⽆法控制字体⼤⼩,并且由于某种原因,运⾏此脚本时打开的控制台⾮常宽.
我不知道ctypes是如何⼯作的-我只想从Python内部修改控制台字体的⼤⼩.
有什么实际可⾏的解决⽅案吗?
import ctypes
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 = 11
font.dwFontSize.Y = 18
font.FontFamily = 54
font.FontWeight = 400
font.FaceName = "Lucida Console"
handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
ctypes.windll.kernel32.SetCurrentConsoleFontEx(
handle, ctypes.c_long(False), ctypes.pointer(font))
print("Foo")
解决⽅法:
我想先指出以下任何⼀个:
我将您的代码更改为“位”.
#!/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_stype = DWORD
get_std_handle_func = kernel32_dll.GetStdHandle
get_std_handle_func.argtypes = [DWORD]
get_std_stype = 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_stype = 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_stype = BOOL
# Get stdout handle
python新手代码错了应该怎么改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.wintypes常量(引⽤标准ctypes类型)(使代码具有Win之类的味道)
>这很长(⼏乎很痛苦)长(也是因为我添加了适当的错误处理)
>根据[SO]: Change console font in Windows(从中复制代码的位置)的答案之⼀所建议的⼀种替代⽅法
是,安装第三⽅模块(例如[GitHub]: mhammond/pywin32 – Python for Windows (pywin32) Extensions,它是WINAPI上的Python包装器),这将需要较少的代码编写, Python和C之间的桥接将已经实现,并且可能仅需⼏⾏即可完成上述功能
>正如我在代码中评论的那样,设置COORD.X似乎被忽略了.但是在设置COORD.Y时会⾃动设置(设置为接近COORD.Y // 2的值-可能会保留宽⾼⽐).在我的计算机(Win 10 x64)上,默认值为16.Yo可能希望将其重新设置为最后,以避免使控制台处于“受挑战的”状态(显然,Win将cmd窗⼝⼤⼩调整为)与字体⼤⼩同步):
标签:python,fonts,cmd,ctypes
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论