你不能只使用 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()