2012-06-21 25 views
0

我想創建一個TreeControl,它將是一個包含圖像文件名的側面板。如何生成一個包含wx.treeControl文件路徑列表的樹?如何用文件路徑列表構建wx.TreeControl?

的文件路徑

℃實施例:\程序Files \ Windows邊欄\小工具\ Weather.Gadget \圖像\ 120DPI \(120DPI)alertIcon.png

C:\ Program Files文件\ Windows邊欄\小工具\ Weather.Gadget \ \圖像120DPI \(120DPI)grayStateIcon.png

C:\ Program Files文件\ Windows邊欄\工具\ Weather.Gadget \ \圖像120DPI \(120DPI)greenStateIcon.png

ç :\ Program Files \ Windows Sidebar \ Gadgets \ Weather.Gadget \ images \ 120DPI \(120DPI)notCo nnectedStateIcon.png

C:\ Program Files文件\ Windows邊欄\小工具\ Weather.Gadget \ \圖像144DPI \(144DPI)alertIcon.png

我希望能夠把這些變成使用目錄樹wx.TreeControl

+0

如在每個文件夾的節點或每個完整路徑的節點? – GP89

+0

每個文件夾的節點 – user1401950

+0

[您嘗試過什麼?](http://whathaveyoutried.com) – GP89

回答

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