Controlling the image for each item

The images for the tree controls are managed with an image list in much the same way as images are managed in list controls. For details on creating image lists, see chapter 13. Once you've created the image list, you assign it to the tree control using the method SetImageList(imageList) or AssignImageList(image-List) . The former allows the image list to be shared with other controls, and the latter gives ownership of the image list to the tree control. You can later get the image list using the method GetImageList(). Figure 15.2 displays a sample tree with some images.

Wxpython Artprovider Icons

Figure 15.2

The sample tree with an image list assigned

Figure 15.2

The sample tree with an image list assigned

Listing 15.2 provides the code used for figure 15.2. It uses the art provider object to provide common images.

Listing 15.2 A tree control with icons import wx import data

Creating an image list

Adding images to a list class TestFrame(wx.Frame):

wx.Frame._init_(self, None, title="simple tree with icons", size=(4 00,500)) il = wx.ImageList(16,16) <

self.fldridx = il.Add(

wx.ArtProvider.GetBitmap(wx.ART_FOLDER, wx.ART_OTHER, (16,16))) self.fldropenidx = il.Add(

wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN, wx.ART_OTHER, (16,16))) self.fileidx = il.Add(

wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE, wx.ART_OTHER, (16,16))) self.tree = wx.TreeCtrl(self)

self.tree.AssignlmageList(il) <hJ Attaching a list and tree root = self.tree.AddRoot("wx.Object")

self.tree.SetItemImage(root, self.fldridx, <1— Setting root images wx.TreeItemIcon_Normal)

self.tree.SetItemImage(root, self.fldropenidx, wx.TreeItemIcon_Expanded)

self.AddTreeNodes(root, data.tree) self.tree.Expand(root)

def AddTreeNodes(self, parentItem, items): for item in items:

newItem = self.tree.AppendItem(parentItem, item) self.tree.SetItemImage(newItem, self.fileidx, <1—



newItem = self.tree.AppendItem(parentItem, item[0]) self.tree.SetItemImage(newItem, self.fldridx, <-

wx.TreeItemIcon_Normal) self.tree.SetItemImage(newItem, self.fldropenidx, wx.TreeItemIcon_Expanded)

Setting node self.AddTreeNodes(newItem, item[1])

images def GetItemText(self, item): if item:

return self.tree.GetItemText(item) else:

return ""

app = wx.PySimpleApp(redirect=True)

frame = TestFrame()



As we've seen, when you add an item to the list, you have the option of associating two different images with the item—one for the unselected state and one for when the item is selected. As with list controls, you specify the index of the image you want within the image list. If you want to get the image assigned after the item is created, you can use the method GetItemImage(item, which=wx.Tree-ItemIcon_Normal). The item parameter is the wx.TreeItemId of the item. The which parameter controls which image you get back. With the default value wx.TreeItemIcon_Normal you get the index of the unselected image for the item. Other values for the which parameter are wx.TreeItemIcon_Selected which returns the image in the selected state, and wx.TreeItemIcon_Expanded, and wxTreeItemIcon_SelectedExpanded which return images used when the tree item is expanded. Note that the latter two images cannot be set using the add methods—if you want to set them, you have to do it with the method SetItem-Image(item, image, which=wx.TreeItemIcon_Normal). The item is, again, the wx.TreeItemId of the item under discussion, the image parameter is the integer index of the new image, and the which parameter takes the same values with the same meaning as in the getter method.

Was this article helpful?

0 0

Post a comment