【发布时间】:2014-01-12 02:04:49
【问题描述】:
我目前正试图弄清楚如何创建一些代码,这些代码将输入像“.png”这样的文件扩展名并返回与系统上该文件类型关联的图标。
我正在使用 python 2.7.6 和 Windows 8。我一直在寻找这方面的代码几个小时,通过保存 .exe 文件中的图像但没有在注册表中找到文件扩展名并保存来接近它它。
我找到了一些有效的代码,允许我将文件保存为 bmp,它基本上可以通过使用 wxpython 的图标进行位图工作并保存图像来工作。但是,我希望代码根本不使用 wxpython,因为我使用 Tkinter 对接口本身进行编码。
这是来自http://ginstrom.com/scribbles/2007/08/31/file-list-with-icons-on-wxpython-windows/的当前有效代码(稍作修改)
import wx
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)
root = wx.App()
bitmapFile = extension_to_bitmap(".png")
bitmapFile.SaveFile('test.bmp', wx.BITMAP_TYPE_BMP)
非常感谢任何帮助!
【问题讨论】:
标签: python icons wxpython file-extension pywin32