wxpython tree ctrl 图片

发布于 2024-12-15 04:09:27 字数 2396 浏览 1 评论 0原文

好的,我这里有一个示例:

可运行示例

示例

提取 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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏末染殇 2024-12-22 04:09:27

您的代码有两个问题。

  1. 并非所有图像都是 16 像素 x 16 像素,但您正尝试将它们添加到应仅包含 16 x 16 图像的图像列表中。要解决此问题,您应该在添加到列表之前将它们缩放到 16 x 16。

  2. 在将图像应用于树项目之前,您应该将图像列表分配给树对象。

这是固定代码:

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()

There are 2 problems with you code.

  1. 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.

  2. You should assign image list to tree object before applying images to tree items.

Here is fixed code:

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文