wxpython 读书笔记(0.15)读取目录 生成树状结构图

啄木鸟上面有还在翻译的:http://wiki.woodpecker.org.cn/moin/WxPythonInAction

wxpython中第15节有树状控件说明

  • 加入节点
# -*- coding:UTF-8 -*-
import wx

class TestFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="simple tree", size=(400,500))

        # Create the tree
        self.tree = wx.TreeCtrl(self)

        # Add a root node
        rootID = self.tree.AddRoot("根节点")

        # Expand the first level
        self.tree.Expand(rootID)

app = wx.App()
frame = TestFrame()
frame.Show()
app.MainLoop()

 

# -*- coding:UTF-8 -*-
import wx

class TestFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="simple tree", size=(400,500))

        # Create the tree
        self.tree = wx.TreeCtrl(self)

        # Add a root node
        rootID = self.tree.AddRoot("根节点")
        childID1 = self.tree.AppendItem(rootID, "子节点1级")

        # Expand the first level
        self.tree.Expand(rootID)

app = wx.App()
frame = TestFrame()
frame.Show()
app.MainLoop()

 

# -*- coding:UTF-8 -*-
import wx

class TestFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="simple tree", size=(400,500))

        # Create the tree
        self.tree = wx.TreeCtrl(self)

        # Add a root node
        rootID = self.tree.AddRoot("根节点")
        childID1 = self.tree.AppendItem(rootID, "子节点1级")
        childID2 = self.tree.AppendItem(childID1, "子节点2级")

        # Expand the first level
        self.tree.Expand(rootID)



app = wx.App()
frame = TestFrame()
frame.Show()
app.MainLoop()
  • 取出目录以及子目录数据

os.listdir(dirname):列出dirname下的目录和文件

# -*- coding:UTF-8 -*-
import wx
import os

class TestFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="simple tree", size=(400,500))
        # Create the tree
        self.tree = wx.TreeCtrl(self)
        
        # Add a root node
        rootID = self.tree.AddRoot("根节点")
        for i in os.listdir('/home/bigzhu'):
          childID1 = self.tree.AppendItem(rootID, i)
        self.tree.Expand(rootID)
app = wx.App()
frame = TestFrame()
frame.Show()
app.MainLoop()

 

# -*- coding:utf-8 -*-
import wx
import os

def appendDir(tree, treeID, sListDir):
    """遍历路径,将文件生成节点加入到wx的tree中
        tree wx的tree
        treeID 上级treeID
        sListDir 一个绝对路径,会自动遍历下面的子目录
    """
    #有些目录没有权限访问的,避免其报错
    try:
        ListFirstDir = os.listdir(sListDir)
        for i in ListFirstDir:
            sAllDir = sListDir+"/"+i
            #有些目录名非法,无法生成节点,只有try一把
            try:
                childID = tree.AppendItem(treeID, i)
            except:
                childID = tree.AppendItem(treeID, "非法名称")
            #如果是目录,那么递归
            if os.path.isdir(sAllDir):
                appendDir(tree, childID, sAllDir)
    except:
        pass
class TestFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="simple tree", size=(400,500))
        # Create the tree
        self.tree = wx.TreeCtrl(self)
        # Add a root node
        rootID = self.tree.AddRoot("根节点")
        #appendDir(self.tree,rootID, "/home/bigzhu/code")
        appendDir(self.tree, rootID, "/home/bigzhu/Desktop")
        self.tree.Expand(rootID)

app = wx.PySimpleApp()
frame = TestFrame()
frame.Show()
app.MainLoop()

Posted by bigzhu Aug 17, 2010 01:16:20 AM