Fetch file's associated icon

Thanks, I have studied that example but don’t think it tells me everything I need.

Given a filename:

  • if it’s a binary return the icon embeded in the file

  • if no file embeded return standard exe file icon

  • else

  • return file extension icon

This code does the else clause:

import wx

class MyFrame(wx.Frame):
def init(self, parent=None):
wx.Frame.init(self, parent, wx.ID_ANY)

icon = self.iconLookup(“jpg”)
if icon:
self.SetIcon(icon)

Look up a given file extension or MIME type.

def iconLookup(self, ext):
    txt = ext

fileType = wx.TheMimeTypesManager.GetFileTypeFromExtension(txt)

    if fileType is None:
        print "not found"
    else:
        info = fileType.GetIconInfo

()
if info != None:
icon, file, idx = info
if icon.Ok():
return icon
else:
print “doh”
return None

if name == ‘main’:
app = wx.App(redirect=False)
frame = MyFrame()
frame.Show(True)
app.MainLoop()

So any examples for the first problem would be appriciated. Also if you replace

icon = self.iconLookup(“jpg”)

with

icon = self.iconLookup(“exe”)

you get a nasty popup instead of the standard exe icon. Anybody knows why?

···

On 6/19/06, Andrea Gavana andrea.gavana@gmail.com wrote:

Hello Rune,

Windows explorer displays an associated icon for any given file. My question
is: Does anybody have example code of how I can do this i wxpython?

You may want to try wx.TheMimeTypesManager. You can find an example of
its use also in the wxPython demo, under “Miscellaneous =>
MimeTypesManager”.

Andrea.

“Imagination Is The Only Weapon In The War Against Reality.”

http://xoomer.virgilio.it/infinity77/


To unsubscribe, e-mail:
wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org


Rune Devik
http://www.garageinnovation.org

Rune Devik wrote:

Thanks, I have studied that example but don't think it tells me everything I need.
Given a filename:
- if it's a binary return the icon embeded in the file
   - if no file embeded return standard exe file icon
  - else
    - return file extension icon
This code does the else clause:

So any examples for the first problem would be appriciated. Also if you

You can use wx.IconLocation to pull icons out of a binary file on Windows. For example, here is some PyShell fun:

  >>> import wx
  >>> il = wx.IconLocation(r'C:\Program Files\Microsoft Office\Office\EXCEL.EXE', 0)
  >>> i = wx.IconFromLocation(il)
  >>> i.Ok()
  True

···

sf = wx.GetTopLevelParent(shell)

  >>> sf.SetIcon(i)

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!