Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在wxPython中更新treectrl_Python_Wxpython_Treecontrol - Fatal编程技术网

在wxPython中更新treectrl

在wxPython中更新treectrl,python,wxpython,treecontrol,Python,Wxpython,Treecontrol,在树目录中显示项目后,如何更改或添加项目。 我创建了一个简单的示例,如何在init之后添加额外的项(例如香蕉)。 在init退出之前更改它是可行的,但我希望能够在已经显示treectrl之后更新它: import wx class TreeFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, title='TreeCtrl') tree_ctrl = wx.TreeCt

在树目录中显示项目后,如何更改或添加项目。 我创建了一个简单的示例,如何在init之后添加额外的项(例如香蕉)。 在init退出之前更改它是可行的,但我希望能够在已经显示treectrl之后更新它:

import wx

class TreeFrame(wx.Frame):

    def __init__(self):

        wx.Frame.__init__(self, None, title='TreeCtrl')

        tree_ctrl = wx.TreeCtrl(self, -1, style=wx.TR_DEFAULT_STYLE | \
                                            wx.TR_FULL_ROW_HIGHLIGHT | \
                                            wx.TR_EDIT_LABELS)

        # Add the tree root
        root = tree_ctrl.AddRoot('Food')
        tree_ctrl.AppendItem(root,'Fruit (3)')
        tree_ctrl.AppendItem(tree_ctrl.GetLastChild(root),'Apple (1)')
        tree_ctrl.AppendItem(tree_ctrl.GetLastChild(root),'Orange (2)')

        tree_ctrl.ExpandAll()
        self.Centre()

    # So how can I change the treectrl above after _init_ .
    # E.g. Add bananas

    print 'do something'



if __name__ == '__main__':
    app = wx.App(0)
    frame = TreeFrame()
    frame.Show()
    app.MainLoop()

我加了一个按钮。如果单击该按钮,香蕉(3)将附加到树上。有关详细信息,请参见注释(特别是
注释:

import wx

class TreeFrame(wx.Frame):

    def __init__(self):

        wx.Frame.__init__(self, None, title='TreeCtrl')
        tree_ctrl = wx.TreeCtrl(self, -1, style=wx.TR_DEFAULT_STYLE | \
                                wx.TR_FULL_ROW_HIGHLIGHT | \
                                wx.TR_EDIT_LABELS)

        # NOTE: Bind callback which will be called when the button is clicked.
        button = wx.Button(self, -1, label='Add banana')
        button.Bind(wx.EVT_BUTTON, self.add_banana)

        # NOTE sizer
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(tree_ctrl, 1, wx.EXPAND|wx.ALL)
        sizer.Add(button, 0, wx.EXPAND|wx.ALL)
        self.SetSizer(sizer)


        # Add the tree root
        root = tree_ctrl.AddRoot('Food')
        tree_ctrl.AppendItem(root,'Fruit (3)')
        tree_ctrl.AppendItem(tree_ctrl.GetLastChild(root),'Apple (1)')
        tree_ctrl.AppendItem(tree_ctrl.GetLastChild(root),'Orange (2)')

        tree_ctrl.ExpandAll()
        self.Centre()

         # NOTE: Save tree_ctrl, root as attribute
         #       to make them available in add_banana method.
        self.tree_ctrl = tree_ctrl
        self.root = root

    # called when the button is clicked.
    def add_banana(self, evt):
        self.tree_ctrl.AppendItem(self.tree_ctrl.GetLastChild(self.root), 'Banana (3)')


if __name__ == '__main__':
    app = wx.App(0)
    frame = TreeFrame()
    frame.Show()
    app.MainLoop()