在 Windows 中更改控制台字体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3592673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change console font in Windows
提问by russo
Is there a way to change the console font in Windows in python 2.6?
有没有办法在 python 2.6 中更改 Windows 中的控制台字体?
I'm on Windows 7.
我在 Windows 7 上。
ie:
IE:
import os
os.console.font = 'Lucida Console'
*EDIT(posted this an answer by accident)
*编辑(不小心发布了这个答案)
Some more information and questions:
更多信息和问题:
I looked into the windows API: http://msdn.microsoft.com/en-us/library/ms682073%28v=VS.85%29.aspx
我查看了 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
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/
我的下一步是找到一个可以使用 windows API 的 python 模块。这是一个叫做 pywin32 的:http: //sourceforge.net/projects/pywin32/
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')
您导入的实际模块不称为 pywin32,而是 win32api、win32net、win32console 我通过完全猜测弄清楚了这一点。文档在哪里?运行帮助('win32console')
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...?
那里没有显示提到的字体功能,只是缺少它们。我在这里错过了什么吗?文档在哪里?或者具有所有 API 控制台功能的模块在哪里......?
回答by Marfisa
It is possible to change the console font using ctypes
. A minimal code example would look like this:
可以使用 更改控制台字体ctypes
。一个最小的代码示例如下所示:
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.
我还在我的主页上写了一个不太简单的例子。
回答by Redouane Zait
you might wanna check http://pypi.python.org/pypi/colorama
回答by Krzysztof Kowalczyk
Probably not. In Windows console font is the property of and managed by the cmd.exe program.
可能不是。在 Windows 控制台中,字体是 cmd.exe 程序的属性并由其管理。
As with everything, it's possible that if you reverse engineer how cmd.exe works, where it stores information about the font, how to force it to reload it etc. you might be able to do hack it (in any language) but there is no functionality provided by the system in a supported and documented way on how to do it.
与所有事情一样,如果您对 cmd.exe 的工作方式、它存储有关字体的信息的位置、如何强制它重新加载等进行逆向工程,您可能能够对其进行破解(以任何语言),但是有系统没有以支持和记录的方式提供有关如何操作的功能。
回答by russo
I looked into the windows API: http://msdn.microsoft.com/en-us/library/ms682073%28v=VS.85%29.aspx
我查看了 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
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/
我的下一步是找到一个可以使用 windows API 的 python 模块。这是一个叫做 pywin32 的:http: //sourceforge.net/projects/pywin32/
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')
您导入的实际模块不称为 pywin32,而是 win32api、win32net、win32console 我通过完全猜测弄清楚了这一点。文档在哪里?运行帮助('win32console')
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...?
那里没有显示提到的字体功能,只是缺少它们。我在这里错过了什么吗?文档在哪里?或者具有所有 API 控制台功能的模块在哪里......?
回答by cji
Well, I haven't dig deep enough to be able to choose font by name (and I doubt it is possible), but this code (provided that pywin32 is installed) seems to do something funny with it's console (must be cmd.exe, Console2 doesn't work, I don't know if it works with powershell):
好吧,我还没有深入挖掘到能够按名称选择字体(我怀疑这是可能的),但是这段代码(假设安装了 pywin32)似乎对它的控制台做了一些有趣的事情(必须是 cmd.exe ,Console2不行,不知道powershell能不能用):
[C:Users/cji]|1> import win32console
[C:Users/cji]|2> win32console.PyConsoleScreenBufferType( win32console.GetStdHandle( win32console.STD_OUTPUT_HANDLE ) )
<2> <PyConsoleScreenBuffer:19>
[C:Users/cji]|3> p = _
[C:Users/cji]|6> p.SetConsoleFont( 1 )
[C:Users/cji]|7> p.SetConsoleFont( 2 )
# and so on, to:
[C:Users/cji]|12> p.SetConsoleFont( 11 ) #this is Lucida Console, if I see correctly
Documentation says, that SetConsoleFont
"is not documented on MSDN"... But, it certainly does somethingwith current console font, so I think you should search in this direction.
文档说,SetConsoleFont
“MSDN 上没有记录”......但是,它肯定对当前的控制台字体做了一些事情,所以我认为你应该朝这个方向搜索。
Also, similar question: How can I change console font?
另外,类似的问题:如何更改控制台字体?
回答by emilast
If anyone reads this having the problem that setting the default font for PowerShell prompts to Lucida Consoledoes not work, there can be several reasons (many of the related to only that particular font). I've blogged about it here: http://www.meadow.se/wordpress/setting-the-font-of-a-powershell-console-to-lucida-console-wont-work/
如果有人在阅读本文时遇到将 PowerShell 提示的默认字体设置为Lucida 控制台不起作用的问题,则可能有多种原因(许多仅与该特定字体相关)。我在这里写了博客:http: //www.meadow.se/wordpress/setting-the-font-of-a-powershell-console-to-lucida-console-wont-work/
In short, for me it was necessary to change system locale from Swedish to English (United States)but there are several other possible solutions as well.
简而言之,对我来说,有必要将系统区域设置从瑞典语更改为英语(美国),但还有其他几种可能的解决方案。
Hope this helps.
希望这可以帮助。
Emil
埃米尔
回答by leoluk
It is impossible to change it for one session because the font setting is system-wide.
由于字体设置是系统范围的,因此不可能在一次会话中更改它。
You can change the global font by changing some values in the registry, but you'll have to reboot the system.
您可以通过更改注册表中的某些值来更改全局字体,但您必须重新启动系统。