wxPython - wx.TreeCtrl 中的 Expand() 方法
在本文中,我们将学习与 wxPython 的 wx.TreeCtrl 类关联的 Expand() 方法。 Expand() 方法用于扩展和显示树控件中特定项目的子节点。
该函数将树节点项作为我们要扩展到的参数。
Syntax: wx.TreeCtrl.Expand(self, item)
参数:Parameters Type Description item wx.TreeItemId Item that we want to associate editlabel with.
代码示例:
Python
import wx
class MyTree(wx.TreeCtrl):
def __init__(self, parent, id, pos, size, style):
wx.TreeCtrl.__init__(self, parent, id, pos, size, style)
class TreePanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
# create tree control in window
self.tree = MyTree(self, wx.ID_ANY, wx.DefaultPosition,
wx.DefaultSize, wx.TR_HAS_BUTTONS)
# CREATE TREE ROOT
self.root = self.tree.AddRoot('root')
self.tree.SetPyData(self.root, ('key', 'value'))
# add item to root
item = self.tree.AppendItem(self.root, "Item")
item2 = self.tree.AppendItem(self.root, "Item")
# expand root
self.tree.Expand(self.root)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.tree, 0, wx.EXPAND)
self.SetSizer(sizer)
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, parent = None, title ='TreeCtrl Demo')
panel = TreePanel(self)
self.Show()
if __name__ == '__main__':
app = wx.App(redirect = False)
frame = MainFrame()
app.MainLoop()
输出: