[출처 : http://www.imp17.com/tc/myevan/20?category=10]
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()
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()