如何解决在python中结合“控制台缓冲”和“以编程方式更改字体大小”
这是两个问题 font size 和 console buffering 的组合。我知道如何分开做,但不能一起做。这是字体大小的代码:
def changeFontSize(size=2):
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",]
class CONSOLE_FONT_INFOEX(Structure):
_fields_ = [
("cbSize",ULONG),("nFont",DWORD),("dwFontSize",COORD),("FontFamily",UINT),("FontWeight",("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,POINTER(CONSOLE_FONT_INFOEX)]
set_current_console_font_ex_func.restype = BOOL
stdout = get_std_handle_func(STD_OUTPUT_HANDLE)
font = CONSOLE_FONT_INFOEX()
font.cbSize = sizeof(CONSOLE_FONT_INFOEX)
font.dwFontSize.X = size
font.dwFontSize.Y = size
set_current_console_font_ex_func(stdout,False,byref(font))
#TEST
changeFontSize()
import time
time.sleep(10)
这里是缓冲代码:
import win32console,win32con,time #pip install pypiwin32
myConsole = win32console.CreateConsoleScreenBuffer(DesiredAccess = win32con.GENERIC_READ | win32con.GENERIC_WRITE,ShareMode=0,SecurityAttributes=None,Flags=1) # create screen buffer
myConsole.SetConsoleActiveScreenBuffer() # set this buffer to be active
print = lambda x: myConsole.WriteConsole(x) # Effectively the print func.
#TEST
print("hello!")
time.sleep(10)
我想同时使用两者,但问题是缓冲设置了一个新的控制台来控制字体大小更改方法无权访问。如何同时使用两者?
(这里是错误信息的一些测试代码:pywintypes.error: (6,'CreateConsoleScreenBuffer','The handle is invalid.')
)
import win32console,Flags=1) # create screen buffer
myConsole.SetConsoleActiveScreenBuffer() # set this buffer to be active
print = lambda x: myConsole.WriteConsole(x) # Effectively the print func.
changeFontSize(size=2)
print("hellloooooo")
time.sleep(10)
def changeFontSize(size=2):
from ctypes import POINTER,byref(font))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。