📌  相关文章
📜  wxPython – Collapse() 方法 wx.TreeCtrl

📅  最后修改于: 2022-05-13 01:54:19.908000             🧑  作者: Mango

wxPython – Collapse() 方法 wx.TreeCtrl

在本文中,我们将学习与 wxPython 的 wx.TreeCtrl 类关联的 Collapse() 方法。 Collapse() 方法用于简单地折叠给定项目,即根项目被折叠而子项目变得不可见。
Collapse() 方法采用 wx.TreeItemId 参数。

代码示例:

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
        self.tree = MyTree(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize,
                                                             wx.TR_HAS_BUTTONS)
  
        self.btn = wx.Button(self, 1, "Collapse", (50, 60))
  
        # add root to self.tree
        self.root = self.tree.AddRoot('Root')
        # add item to self.root
        item = self.tree.AppendItem(self.root, 'Item')
  
        # bind event with self.btn
        self.btn.Bind(wx.EVT_BUTTON, self.onclick)
  
        self.tree.Expand(self.root)
  
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.tree, 0, wx.EXPAND)
        self.SetSizer(sizer)
  
    def onclick(self, e):
        # collapse root
        self.tree.Collapse(self.root)
  
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()

输出窗口:

在点击按钮之前


点击按钮后