File list with icons on wxPython/Windows
I recently wanted to show a list of files in wxPython, with the file icons (on Windows). I found the answer in this post by Mark Hammond, then had to search around a bit to find how to get the icon handle into a wxPython bitmap for the image list. I thought I'd save any future searchers the trouble.
Here's the meat of the code:
from win32com.shell import shell, shellcon
from win32con import FILE_ATTRIBUTE_NORMAL
def extension_to_bitmap(extension):
"""dot is mandatory in extension"""
flags = shellcon.SHGFI_SMALLICON | \
shellcon.SHGFI_ICON | \
shellcon.SHGFI_USEFILEATTRIBUTES
retval, info = shell.SHGetFileInfo(extension,
FILE_ATTRIBUTE_NORMAL,
flags)
# non-zero on success
assert retval
hicon, iicon, attr, display_name, type_name = info
# Get the bitmap
icon = wx.EmptyIcon()
icon.SetHandle(hicon)
return wx.BitmapFromIcon(icon)
Here is a python file with a subclass of wx.ListCtrl that shows a list of files, and a simple demo as proof of concept.

