wxpython tree ctrl 图片
好的,我这里有一个示例:
可运行示例
提取 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 演示应用程序中的演示进行操作,但没有成功。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码有两个问题。
并非所有图像都是 16 像素 x 16 像素,但您正尝试将它们添加到应仅包含 16 x 16 图像的图像列表中。要解决此问题,您应该在添加到列表之前将它们缩放到 16 x 16。
在将图像应用于树项目之前,您应该将图像列表分配给树对象。
这是固定代码:
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: