我试图在我的面板中创建一个树 ctrl 组件,但是当我运行代码时,我没有看到任何操作。
代码
import wx
class MyTreeCtrl(wx.TreeCtrl):
def __init__(self, parent, id, pos, size, style):
wx.TreeCtrl.__init__(self, parent, id, pos, size, style)
class Example(wx.Frame):
def __init__(self, parent, title):
super(Example, self).__init__(parent, title=title,
size=(700, 700))
tID = wx.NewId()
self.tree = MyTreeCtrl(self,tId,wx.DefaultPosition,wx.DefaultSize,wx.TR_DEFAULT_SIZE)
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))
fileidx = il.Add(wx.ArtProvider_GetBitmap(wx.ART_NORMAL_FILE, wx.ART_OTHER, isz))
smileidx = il.Add(images.Smiles.GetBitmap())
self.tree.SetImageList(il)
self.il = il
# NOTE: For some reason tree items have to have a data object in
# order to be sorted. Since our compare just uses the labels
# we don't need any real data, so we'll just use None below for
# the item data.
self.root = self.tree.AddRoot("The Root Item")
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 x in range(15):
child = self.tree.AppendItem(self.root, "Item %d" % x)
self.tree.SetPyData(child, None)
self.tree.SetItemImage(child, fldridx, wx.TreeItemIcon_Normal)
self.tree.SetItemImage(child, fldropenidx, wx.TreeItemIcon_Expanded)
for y in range(5):
last = self.tree.AppendItem(child, "item %d-%s" % (x, chr(ord("a")+y)))
self.tree.SetPyData(last, None)
self.tree.SetItemImage(last, fldridx, wx.TreeItemIcon_Normal)
self.tree.SetItemImage(last, fldropenidx, wx.TreeItemIcon_Expanded)
for z in range(5):
item = self.tree.AppendItem(last, "item %d-%s-%d" % (x, chr(ord("a")+y), z))
self.tree.SetPyData(item, None)
self.tree.SetItemImage(item, fileidx, wx.TreeItemIcon_Normal)
self.tree.SetItemImage(item, smileidx, wx.TreeItemIcon_Selected)
print self.tree
self.InitUI()
self.Centre()
self.Show()
def InitUI(self):
panel = wx.Panel(self)
panel.SetBackgroundColour('#4f5049')
vbox = wx.BoxSizer(wx.VERTICAL)
#midPan = wx.Panel(panel)
#midPan.SetBackgroundColour('#ededed')
vbox.Add(self.tree, 1, wx.EXPAND | wx.ALL, 20)
panel.SetSizer(vbox)
if __name__ == '__main__':
app = wx.App()
Example(None,title="Border")
app.MainLoop()