Change console font in Windows

后端 未结 7 1128
闹比i
闹比i 2020-11-30 12:04

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

I\'m on Windows 7.

ie:

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


        
相关标签:
7条回答
  • 2020-11-30 12:18

    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
    

    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/

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

    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...?

    0 讨论(0)
  • 2020-11-30 12:19

    If anyone reads this having the problem that setting the default font for PowerShell prompts to Lucida Console does 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/

    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

    0 讨论(0)
  • 2020-11-30 12:22

    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.

    0 讨论(0)
  • 2020-11-30 12:28

    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.

    0 讨论(0)
  • 2020-11-30 12:30

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

    [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 something with current console font, so I think you should search in this direction.

    Also, similar question: How can I change console font?

    0 讨论(0)
  • 2020-11-30 12:36

    Probably not. In Windows console font is the property of and managed by the cmd.exe program.

    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.

    0 讨论(0)
提交回复
热议问题