📌  相关文章
📜  wxPython - 两步创建 wx.TreeCtrl

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

wxPython - 两步创建 wx.TreeCtrl

在本文中,我们将学习如何使用两步创建来创建树控件。为此,我们将在 wx.TreeCtrl 类中使用 Create() 方法。基本上,我们将使用带有空括号的 TreeCtrl() 构造函数初始化树控件,然后我们将使用 Create() 方法和属性与树控件关联。

代码示例:

Python
import wx
 
class TreePanel(wx.Panel):
 
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
 
        # initialize Tree Control
        self.tree = wx.TreeCtrl(self, wx.ID_ANY, wx.DefaultPosition, (100, 70),
                           wx.TR_HAS_BUTTONS)
 
        # create Tree Control using Create() method
        self.tree.Create
        # Add root to Tree Control
        self.root = self.tree.AddRoot('Root')
 
        # Add item to root
        itm = self.tree.AppendItem(self.root, 'Item')
 
        # Add item to 'itm'
        self.tree.AppendItem(itm, "Sub Item")
 
        # Expand whole tree
        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()


输出: