我目前正在制作一个终端应用程序,并希望允许用户更改 Windows 11 终端的缩放。有没有办法改变字体大小或其他方式?
因为我没有找到任何东西可以做到这一点,所以我尝试使用 pynput “手动”执行此操作,但它不起作用。我寻找无需用户手动更改字体大小的方法。我也尝试过 ANSI 转义码,但也不起作用。 (请不要介意以下脚本中是否有错误,因为我删除了所有过去的尝试)
from pynput.keyboard import Controller, Key
keyboard = Controller()
while True:
x,y = get_terminal_size()
print(f"Actual setting : {x}x{y}")
ch = input("Increase or decrease quality (+/-)(enter to exit) : ")
if ch == "+":
with keyboard.pressed(Key.ctrl):
Press(KEYPAD_MINUS)
elif ch == "-":
with keyboard.pressed(Key.ctrl):
Press(KEYPAD_PLUS)
elif ch == "":
break
else:
print("")
from pynput.keyboard import Controller, Key
keyboard = Controller()
while True:
x,y = get_terminal_size()
print(f"Actual setting : {x}x{y}")
ch = input("Increase or decrease quality (+/-)(enter to exit) : ")
if ch == "+":
size += 10
print(f"\033[{size}m", end='')
elif ch == "-":
size -= 10
print(f"\033[{size}m", end='')
elif ch == "":
break
else:
print("")```
以下代码对我有用:
from pynput.keyboard import Controller, Key
keyboard = Controller()
while True:
x,y = get_terminal_size()
print(f"Actual setting : {x}x{y}")
ch = input("Increase or decrease quality (+/-)(enter to exit) : ")
if ch == "+":
with keyboard.pressed(Key.ctrl):
keyboard.press('+')
print('increased')
elif ch == "-":
with keyboard.pressed(Key.ctrl):
keyboard.press('-')
print('decreased')
elif ch == "":
break
else:
print("unknown command")
与问题中的代码相比,上面的代码使用
keyboard.press('+')
而不是 KEYPAD_MINUS
看起来很重要。
这是终端中的会话:
(venv_pynput) PS ...\venv_pynput> python .\my_scripts\so_79020368.py
Actual setting : 144x38
Increase or decrease quality (+/-)(enter to exit) : +
increased
Actual setting : 144x38
Increase or decrease quality (+/-)(enter to exit) : +
increased
Actual setting : 134x35
Increase or decrease quality (+/-)(enter to exit) : -
decreased
Actual setting : 125x33
Increase or decrease quality (+/-)(enter to exit) : -
decreased
Actual setting : 134x35
Increase or decrease quality (+/-)(enter to exit) :
(venv_pynput) PS ...\venv_pynput>