我想创建一个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
将它们放入目录树中答案 0 :(得分: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()