好的我在这里有一个例子:
可运行示例
解压缩zip文件然后运行,否则它将无法正常工作
然而,当运行时,图像将不会被添加到树ctrl中,它只会出错。
代码(注意不会在没有图像的情况下运行,请参阅上面的zip文件)
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()
为什么?
我在wxPython演示应用程序中关注了演示,但没有运气。
答案 0 :(得分:2)
您的代码有两个问题。
并非所有图像都是16像素x 16像素,但您尝试将它们添加到应仅包含16 x 16图像的图像列表中。要解决此问题,您应该在添加到列表之前将它们缩放到16 x 16。
在将图像应用于树项之前,您应该将图像列表分配给树对象。
这是固定代码:
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).Scale(16,16).ConvertToBitmap())
popular = image_list.Add(wx.Image("images/popular (Custom).png", wx.BITMAP_TYPE_PNG).Scale(16,16).ConvertToBitmap())
search = image_list.Add(wx.Image("images/search (Custom).png", wx.BITMAP_TYPE_PNG).Scale(16,16).ConvertToBitmap())
download = image_list.Add(wx.Image("images/download (Custom).png", wx.BITMAP_TYPE_PNG).Scale(16,16).ConvertToBitmap())
pandora = image_list.Add(wx.Image("images/playlist_icon (Custom).png", wx.BITMAP_TYPE_PNG).Scale(16,16).ConvertToBitmap())
stations = image_list.Add(wx.Image("images/stations (Custom).png", wx.BITMAP_TYPE_PNG).Scale(16,16).ConvertToBitmap())
self.tree.AssignImageList(image_list)
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()