wxPython PathTreeCtrl

Posted at 2010/05/26 13:26// Posted in wxPython/wxTreeCtrl
트리 컨트롤은 디렉토리 구조를 표현할때 편리합니다.

사용자 삽입 이미지
















그런데 경로를 파싱해서 디렉토리를 만드는건 의외로 귀찮은 일이더라구요 = =)~

그래서 만들어본 예제입니다



# vi: set sw=4 sts=4 expandtab:
import wx
import os

class wxPathTreeCtrl(wx.TreeCtrl):
def __init__(self, *args, **kwargs):
wx.TreeCtrl.__init__(self, *args, **kwargs)

def InitRoot(self, rootPath):
self.branchd = {}
self.DeleteAllItems()
self.root = self.AddRoot(rootPath)
self.SetPyData(self.root, ("ROOT", rootPath))
return self.root

def AppendPath(self, path):
branch = path

branches = []
while branch:
branch, leaf = os.path.split(branch)
if branch in self.branchd:
break
elif branch:
branches.append(branch)

branches.reverse()

last = self.branchd.get(branch, self.root)
for branch in branches:
last = self.AppendItem(last, os.path.split(branch)[1])
self.SetPyData(last, ("DIR", branch))
self.branchd[branch] = last

item = self.AppendItem(last, os.path.split(path)[1])
self.SetPyData(item, ("FILE", path))
return item

def ExpandAllDirs(self):
self.ExpandDirs(self.root)

def ExpandDirs(self, node):
if self.GetPyData(node)[0] == "FILE":
pass
else:
children = list(self.GenChildren(node))
fileCount = len([child for child in children if self.GetPyData(child)[0] == "FILE"])
if 0 == fileCount:
self.Expand(node)

for child in children:
self.ExpandDirs(child)

def GetBranchd(self):
return self.branchd

def GenChildren(self, node):
child = self.GetFirstChild(node)[0]
while child:
yield child
child = self.GetNextSibling(child)

if __name__ == "__main__":

class TestFrame(wx.Frame):
def __init__(self, parent, title, size=(800, 600)):
wx.Frame.__init__(self, parent, -1, title, pos=(0, 0), size=size)
self.CentreOnScreen(wx.BOTH)

self.pathTreeCtrl = wxPathTreeCtrl(self)
self.pathTreeCtrl.InitRoot("root")
self.pathTreeCtrl.AppendPath("bin/main.exe")
self.pathTreeCtrl.AppendPath("data/char/pc/warrior/warriror.png")
self.pathTreeCtrl.AppendPath("data/char/pc/warrior/warriror2.png")
self.pathTreeCtrl.ExpandAllDirs()

class TestApp(wx.App):
def OnInit(self):
toolFrame = TestFrame(None, "TEST")
toolFrame.Show()
self.SetTopWindow(toolFrame)
return True

TestApp(redirect=False).MainLoop()


이올린에 북마크하기(0) 이올린에 추천하기(0)
2010/05/26 13:26 2010/05/26 13:26
import wx

class TestFrame(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, -1, title, pos=(0, 0), size=(320, 240))
        self.CentreOnScreen(wx.BOTH)

        # CreateTree
        tree = wx.TreeCtrl(self, -1, wx.DefaultPosition, wx.DefaultSize, wx.TR_HAS_BUTTONS|wx.TR_LINES_AT_ROOT)

        # AddRoot
        root = tree.AddRoot("root")

        # AddChild
        node = tree.AppendItem(root, "node1")
        node = tree.AppendItem(root, "node2")

        # ExpandTree
        tree.Expand(root)

        tree.Bind(wx.EVT_TREE_ITEM_RIGHT_CLICK, self.__OnRightClickItem)

        self.tree = tree
        self.root = root

    def __OnRightClickItem(self, event):
        print "RClickItem"
        self.tree.Delete(event.GetItem())
        pass

class TestApp(wx.App):
    def OnInit(self):
        "OnInit"
        frame = TestFrame(None, "TestApp")
        frame.Show()
        self.SetTopWindow(frame)
        return True

    def OnExit(self):
        "OnExit"
        pass

TestApp(redirect=False).MainLoop()
이올린에 북마크하기(0) 이올린에 추천하기(0)
2007/05/25 11:13 2007/05/25 11:13

wx파이썬 wx.TreeCtrl 레퍼런스

Posted at 2007/05/22 15:07// Posted in wxPython/wxTreeCtrl

wxTreeCtrl

트리 컨트롤은 계층적인 정보를 보여주며, 각 아이템에 덧붙여진 아이템을 펼쳐 볼 수 있습니다. 트리 컨트롤내 각 아이템은 wx.TreeItemId 핸들로 참조할 수 있으며, 각 아이디의 유효성은 wx.TreeCtrlId.IsOk 로 확인해 볼 수 있습니다.

트리 컨트롤의 이벤트를 핸들링하기 위해서는 wx.TreeEvent 내 이벤트 테이블을 사용하면 됩니다.


wxTreeCtrl()

more..


wxTreeCtrl(parent, id, pos = wxDefaultPosition, size = wxDefaultSize, style = wxTR_HAS_BUTTONS, validator = wxDefaultValidator, name = "treeCtrl")

more..


wxTreeCtrl::AddRoot

more..


wxTreeCtrl::AppendItem

more..


wxTreeCtrl::DeleteAllItems

more..


wxTreeCtrl::Delete 

more..


wxTreeCtrl::ExpandAll 

more..



이벤트 핸들링

more..


계속....


이올린에 북마크하기(0) 이올린에 추천하기(0)
2007/05/22 15:07 2007/05/22 15:07

wx파이썬 트리 컨트롤

Posted at 2007/05/03 14:45// Posted in wxPython/wxTreeCtrl
import wx

TREEICON_ROOT = 0
TREEICON_NODE = 1

class TestFrame(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, -1, title, pos=(0, 0), size=(320, 240))
        self.CentreOnScreen(wx.BOTH)

        # CreateTree
        tree = wx.TreeCtrl(self, -1, wx.DefaultPosition, wx.DefaultSize, wx.TR_HAS_BUTTONS|wx.TR_LINES_AT_ROOT)

        # LoadTreeIcons
        treeIcons = self.__LoadTreeIcons("res/treeimg.bmp")
        tree.SetImageList(treeIcons)

        # AddRoot
        root = tree.AddRoot("root")
        tree.SetPyData(root, None)
        tree.SetItemImage(root, TREEICON_ROOT, wx.TreeItemIcon_Normal)
        tree.SetItemImage(root, TREEICON_ROOT, wx.TreeItemIcon_Expanded)

        # AddChild
        node = tree.AppendItem(root, "node1")
        tree.SetPyData(node, None)
        tree.SetItemImage(node, TREEICON_NODE, wx.TreeItemIcon_Normal)
        tree.SetItemImage(node, TREEICON_NODE, wx.TreeItemIcon_Expanded)

        node = tree.AppendItem(root, "node2")
        tree.SetPyData(node, None)
        tree.SetItemImage(node, TREEICON_NODE, wx.TreeItemIcon_Normal)
        tree.SetItemImage(node, TREEICON_NODE, wx.TreeItemIcon_Expanded)

        # ExpandTree
        tree.Expand(root)

        self.treeIcons = treeIcons
        self.tree = tree
        self.root = root

    def __LoadTreeIcons(self, fileName):
        icon_width = 16
        icon_height = 16

        img = wx.Image(fileName)
        img.SetMaskColour(255, 0, 255)
        img.SetMask(True)

        bmp = wx.BitmapFromImage(img)

        self.bmp = bmp

        imgList = wx.ImageList(icon_width, icon_height)

        count = bmp.GetWidth() // icon_width

        x = 0
        for i in xrange(count):
            icon = bmp.GetSubBitmap(wx.Rect(x, 0, icon_width, icon_height))
            x += icon_width

            imgList.Add(icon)

        return imgList

class TestApp(wx.App):
    def OnInit(self):
        "OnInit"
        frame = TestFrame(None, "TestApp")
        frame.Show()
        self.SetTopWindow(frame)
        return True

    def OnExit(self):
        "OnExit"
        pass

TestApp(redirect=False).MainLoop()
이올린에 북마크하기(0) 이올린에 추천하기(0)
2007/05/03 14:45 2007/05/03 14:45