在 Windows 中更改控制台字体 [英] Change console font in Windows

查看:58
本文介绍了在 Windows 中更改控制台字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 python 2.6 中更改 Windows 中的控制台字体?

Is there a way to change the console font in Windows in python 2.6?

我使用的是 Windows 7.

I'm on Windows 7.

即:

import os
os.console.font = 'Lucida Console'

*EDIT(无意中发布了这个答案)

*EDIT (posted this an answer by accident)

更多信息和问题:

我查看了 Windows API:http://msdn.microsoft.com/en-us/library/ms682073%28v=VS.85%29.aspx

I looked into the windows API: http://msdn.microsoft.com/en-us/library/ms682073%28v=VS.85%29.aspx

好像有修改控制台字体的功能:

It look like it has a function for changing the console font:

SetCurrentConsoleFontEx

或者至少获取有关当前字体的信息:

or at least getting information about the current font:

GetCurrentConsoleFont
GetCurrentConsoleFontEx

我的下一步是找到一个可以使用 windows API 的 python 模块.这是一个名为 pywin32 的:http://sourceforge.net/projects/pywin32/

My next step was to find a python module that I can use the windows API. Here's one called pywin32: http://sourceforge.net/projects/pywin32/

你实际导入的模块不是叫pywin32,而是win32api、win32net、win32console我通过完全猜测弄清楚了这一点.文档在哪里?运行 help('win32console')

The actual modules you import are not called pywin32, but win32api, win32net, win32console I figured this out by complete guesswork. Where's the documentation? a run on help('win32console')

没有在那里显示提到的字体功能,只是缺少它们.我在这里错过了什么吗?文档在哪里?或者哪里有一个包含所有 API 控制台功能的模块?

DOESN'T show the mentioned font functions in there, it's just plain missing them. Am I missing something here? Where are the docs? Or where is a module that has all of the API's console functions...?

推荐答案

可以使用 ctypes 更改控制台字体.一个最小的代码示例如下所示:

It is possible to change the console font using ctypes. A minimal code example would look like this:

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))

我还写了一个不那么简单的例子 在我的主页上.

I also wrote a less minimal example on my homepage.

这篇关于在 Windows 中更改控制台字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆