pywin32: EnumFontFamilies breaks python
To wit:
import win32gui
def enum_fonts(typeface=None):
hwnd = win32gui.GetDesktopWindow()
dc = win32gui.GetWindowDC(hwnd)
res = []
def callback(*args):
res.append(args)
win32gui.EnumFontFamilies(dc, typeface, callback)
win32gui.ReleaseDC(hwnd, dc)
return res
res = enum_fonts()
for r in res:
print r[0].lfF开发者_StackOverflow中文版aceName
What follows is a bizarre aberration:
System
Terminal
Fixedsys
Roman
Script
Modern
Small Fonts
MS Serif
WST_Czec
WST_Engl
WST_Fren
WST_Germ
WST_Ital
WST_Span
WST_Swed
Courier
MS Sans Serif
Marlett
Arial
...
Waker
TT Anvers Black
TT Anvers
wodSymbols
Traceback (most recent call last):
File "test.py", line 48, in <module>
for r in res:
TypeError: an integer is required
It broke python!
Am I making an incorrect assumption with the way I'm calling the function? Is this a known bug in pywin32? Is there any other way to enumerate font families from python?
Ah I will shoot myself. The callback has to return an integer - 0 to stop iterating, non-zero to continue. This works fine:
def enum_fonts(typeface=None):
hwnd = win32gui.GetDesktopWindow()
dc = win32gui.GetWindowDC(hwnd)
res = []
def callback(*args):
res.append(args)
return 1
win32gui.EnumFontFamilies(dc, typeface, callback)
win32gui.ReleaseDC(hwnd, dc)
return res
精彩评论