Skip to content

2010年02月1日

在wxpython的TreeCtrl中使用Windows系统文件图标

最近在使用wxpython制作类资源管理器的文件树,为了更直观显示文件以及文件夹,想使用系统图标,,效果:

在wxpython中不能直接得到系统图标,经过查阅可以使用windows API函数SHGetFileInfo得到图标的resource,然后转成wxBitmap。

关键代码片段:

from win32com.shell import shell, shellcon
from win32con import FILE_ATTRIBUTE_NORMAL
import win32con

def GetFileIcon(path):
	flags = shellcon.SHGFI_SMALLICON | shellcon.SHGFI_ICON
	retval, info = shell.SHGetFileInfo(path,FILE_ATTRIBUTE_NORMAL,flags)

	assert retval

	hicon, iicon, attr, display_name, type_name = info

	# Get the bitmap
	icon = wx.EmptyIcon()
	icon.SetHandle(hicon)
	return wx.BitmapFromIcon(icon)

变量path是字符串,标示路径,比如”d:\”,这样就可以得到D盘的图标数据;也可以是某个文件”D:\file.py”,得到的是此文件的图标数据。值得注意的是,这里面的路径分隔符必须是单个”/”,否者就会报错。
这样就可以加到TreeCtrl控件的ImageList对象中进行显示方面的处理,这块不用多说。
效果就是上图所示了。

Read more from Python, 笔记

Share your thoughts, post a comment.

(required)
(required)

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments