0

我想创建一个 TreeControl,它将是一个包含图像文件名的侧面板。如何生成带有 wx.treeControl 文件路径列表的树?

文件路径示例

C:\Program Files\Windows Sidebar\Gadgets\Weather.Gadget\images\120DPI\(120DPI)alertIcon.png

C:\Program Files\Windows Sidebar\Gadgets\Weather.Gadget\images\120DPI\(120DPI)grayStateIcon.png

C:\Program Files\Windows Sidebar\Gadgets\Weather.Gadget\images\120DPI\(120DPI)greenStateIcon.png

C:\Program Files\Windows Sidebar\Gadgets\Weather.Gadget\images\120DPI\(120DPI)notConnectedStateIcon.png

C:\Program Files\Windows Sidebar\Gadgets\Weather.Gadget\images\144DPI\(144DPI)alertIcon.png

我希望能够使用 wx.TreeControl 将它们放入目录树中

4

1 回答 1

0

你不能只使用 GenericDirCtrl 或者 MultiDirDialog 吗?如果您真的想做自己的事情,那么这个愚蠢的示例应该可以帮助您入门:

import glob
import os
import wx

########################################################################
class MyTreeCtrl(wx.TreeCtrl):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.TreeCtrl.__init__(self, parent)


########################################################################
class MyPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent=parent)

        path = r'C:\Users\mdriscoll\Documents'
        paths = glob.glob(path + "/*")

        self.tree = MyTreeCtrl(self)

        isz = (16,16)
        il = wx.ImageList(isz[0], isz[1])
        fldridx = il.Add(wx.ArtProvider_GetBitmap(wx.ART_FOLDER, 
                                                  wx.ART_OTHER, isz))
        fldropenidx = il.Add(wx.ArtProvider_GetBitmap(wx.ART_FILE_OPEN,
                                                      wx.ART_OTHER, isz))
        self.tree.SetImageList(il)

        self.root = self.tree.AddRoot(os.path.basename(path))
        self.tree.SetPyData(self.root, None)
        self.tree.SetItemImage(self.root, fldridx, wx.TreeItemIcon_Normal)
        self.tree.SetItemImage(self.root, fldropenidx, wx.TreeItemIcon_Expanded)

        for item in paths:
            if os.path.isdir(item):
                child = self.tree.AppendItem(self.root, os.path.basename(item))
                self.tree.SetPyData(child, None)
                self.tree.SetItemImage(child, fldridx, wx.TreeItemIcon_Normal)
                self.tree.SetItemImage(child, fldropenidx, wx.TreeItemIcon_Expanded)
        self.tree.Expand(self.root)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.tree, 1, wx.EXPAND)
        self.SetSizer(sizer)

########################################################################
class MyFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        super(MyFrame, self).__init__(None, title="TreeCtrl Example")
        panel = MyPanel(self)
        self.Show()

if __name__ == "__main__":
    app = wx.App()
    frame = MyFrame()
    app.MainLoop()
于 2012-06-22T14:07:19.850 回答