我试图在我的面板中创建一个树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()
答案 0 :(得分:1)
好吧,我明白为什么。代码在当前形式下是不可用的。 “tId”是未定义的变量,而wx.TR_DEFAULT_SIZE不是真正的样式标志。您还可以参考“images”模块,但不要导入它。有一个可运行的例子会很好。
你也不是在问一个问题。什么不起作用?当我编辑代码以使其运行时,我最终得到了这个:
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_HAS_BUTTONS)
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))
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()
这会将树控件创建为左上角的小方框。下一个问题是你将面板堆叠在treectrl的顶部,因为你给了两个小部件框架作为它们的父级,你首先创建了treectrl。我重新安排了你的代码,使面板成为treectrl的父级。见下文:
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))
panel = wx.Panel(self)
panel.SetBackgroundColour('#4f5049')
vbox = wx.BoxSizer(wx.VERTICAL)
tID = wx.NewId()
self.tree = MyTreeCtrl(panel,tID,wx.DefaultPosition,wx.DefaultSize,wx.TR_HAS_BUTTONS)
vbox.Add(self.tree, 1, wx.EXPAND | wx.ALL, 20)
panel.SetSizer(vbox)
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))
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.Centre()
self.Show()
if __name__ == '__main__':
app = wx.App()
Example(None,title="Border")
app.MainLoop()