Ok I have an example here:
Runable Example
Extract the zip files and then run otherwise it won’t work at all
However when run the images won’t be added to there tree ctrl and it will simply error.
Code (Note won’t run without images, see zip file above)
import wx
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1)
self.tree = wx.TreeCtrl(self, style = wx.TR_HIDE_ROOT)
self.root = self.tree.AddRoot("")
gr = self.tree.AppendItem(self.root, "Grooveshark")
pop_r = self.tree.AppendItem(gr, "Popular")
sr = self.tree.AppendItem(gr, "Search")
dr = self.tree.AppendItem(self.root, "Download")
pr = self.tree.AppendItem(self.root, "Pandora")
stat_r = self.tree.AppendItem(pr, "Stations")
image_list = wx.ImageList(16, 16)
grooveshark = image_list.Add(wx.Image("images/grooveshark (Custom).png", wx.BITMAP_TYPE_PNG).ConvertToBitmap())
popular = image_list.Add(wx.Image("images/popular (Custom).png", wx.BITMAP_TYPE_PNG).ConvertToBitmap())
search = image_list.Add(wx.Image("images/search (Custom).png", wx.BITMAP_TYPE_PNG).ConvertToBitmap())
download = image_list.Add(wx.Image("images/download (Custom).png", wx.BITMAP_TYPE_PNG).ConvertToBitmap())
pandora = image_list.Add(wx.Image("images/playlist_icon (Custom).png", wx.BITMAP_TYPE_PNG).ConvertToBitmap())
stations = image_list.Add(wx.Image("images/stations (Custom).png", wx.BITMAP_TYPE_PNG).ConvertToBitmap())
self.tree.SetPyData(gr, None)
self.tree.SetItemImage(gr, grooveshark, wx.TreeItemIcon_Normal)
self.tree.SetPyData(pop_r, None)
self.tree.SetItemImage(pop_r, popular, wx.TreeItemIcon_Normal)
self.tree.SetPyData(sr, None)
self.tree.SetItemImage(sr, search, wx.TreeItemIcon_Normal)
self.tree.SetPyData(dr, None)
self.tree.SetItemImage(dr, download, wx.TreeItemIcon_Normal)
self.tree.SetPyData(pr, None)
self.tree.SetItemImage(pr, pandora, wx.TreeItemIcon_Normal)
self.tree.SetPyData(stat_r, None)
self.tree.SetItemImage(stat_r, stations, wx.TreeItemIcon_Normal)
if __name__ == "__main__":
a = wx.App(False)
f = TestFrame()
f.Show()
a.MainLoop()
Why?
I followed the demo in the wxPython demo app and no luck.
There are 2 problems with you code.
Not all of your images are 16px x 16px, but you are trying to add them to image list that should contain only 16 x 16 images. To solve this you should scale them to 16 x 16 before adding to a list.
You should assign image list to tree object before applying images to tree items.
Here is fixed code: