Tkinter fonts setting

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Feb 6 13:53:04 EST 2008


En Wed, 06 Feb 2008 14:02:35 -0200, Unnamed One <noname9968 at gmail.com>  
escribi�:

> jim-on-linux wrote:
>> On Tuesday 05 February 2008 15:22, Unnamed
>> One wrote:
>>
>>> First question - is it possible to set
>>> font to default OS font for window text?
>>> It would be preferable, while on my
>>> Windows XP system Tkinter sets small
>>> Helvetica-style font by default.
>>>
>>> Secondly, can I set font globally (or
>>> specify "default" font for widgets)? In
>>> fact, all I want is to get default OS font
>>> unless (rarely) I need to specify another.
>>
>> Go to:
>> http://www.pythonware.com/library/tkinter/introduction/
>>
>> Read chapter 6,  Widget Styling, there is a
>> section on Fonts which has a sub-section on
>> System Fonts.

None of them is suitable as a default text font. Prior to Tk 8.5, there is  
no way to get the "right" fonts used by the OS/desktop/theme. Tk 8.5 may  
provide that, but I haven't got it yet (see  
http://www.tcl.tk/cgi-bin/tct/tip/145.html)

> Regarding my questions, I guess both are impossible in Tkinter (as I
> didn't found anything about that in any Tkinter documentation), but
> there still must be a way to get the default OS font name and size
> (maybe outside of Tkinter), because it's always advised to use them in
> applications.

Yes. Your second question is easy; google for "tkinter default font".
To obtain the desired font description, on Windows, you can use the  
pywin32 package or ctypes to call the following functions, and then  
configure Tkinter to use the desired font by default.
Use SystemParametersInfo(SPI_GETNONCLIENTMETRICS,...) to obtain a  
NONCLIENTMETRICS structure.
 From it, obtain the lfMessageFont field (this is the default font for  
message boxes; you may be interested on others too, like lfMenuFont). It  
has all the info you need: lfFaceName is the font name, the various flags  
determine the font style, note that "bold" == lfWeight>=500.
Maybe lfHeight it's a bit complicated; it's the height in pixels, to  
convert to points (1/72"):

hdc = GetDC(0)
dpi = GetDeviceCaps(hdc, LOGPIXELSY)
font_size = int(round(abs(lfHeight) * 72 / dpi))
ReleaseDC(0, hdc)


-- 
Gabriel Genellina




More information about the Python-list mailing list