pywin32:EnumFontFamilies导致Python崩溃
也就是说:
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].lfFaceName
接下来是一个奇怪的情况:
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
这让Python坏掉了!
我调用这个函数的方式是不是错了?这是pywin32的一个已知bug吗?有没有其他方法可以从Python中列出字体家族?
1 个回答
3
啊,我快要崩溃了。这个回调函数必须返回一个整数——返回0表示停止迭代,返回非零值表示继续迭代。这样做是没问题的:
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