在本文中,我們將學習與wxPython的wx.TreeCtrl類關聯的Delete()方法。 Delete()函數僅用於從樹中刪除特定項目,它可以是根項目或終端項目。
將生成一個EVT_TREE_DELETE_ITEM事件。
此函數可能導致對GetNextChild的後續調用失敗。
用法:
wx.TreeCtrl.Delete(self, item)
參數:
參數 | 類型 | Description |
item | wx.TreeItemId | 我們想要從樹控件中刪除的項目 |
代碼示例:
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, 150),
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
self.itm = self.tree.AppendItem(self.root, 'Item')
# Add item to 'itm'
self.si1 = self.tree.AppendItem(self.itm, "Sub Item")
# Add another item
self.si2 = self.tree.AppendItem(self.itm, "Another Sub Item")
# Expand whole tree
self.tree.ExpandAll()
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.tree, 0, wx.EXPAND)
self.SetSizer(sizer)
# Add button in frame
self.btn = wx.Button(self, 1, "Delete", (10, 170))
# Bind event function with button
self.btn.Bind(wx.EVT_BUTTON, self.onclick)
def onclick(self, e):
# Delete si2 from the tree
self.tree.Delete(self.si2)
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()
輸出:

在點擊按鈕之前

單擊按鈕後
相關用法
- wxPython wx.RadioBox SetItemToolTip()用法及代碼示例
- wxPython wx.RadioBox SetSelection()用法及代碼示例
- wxPython wx.RadioBox SetString()用法及代碼示例
- wxPython wx.RadioBox ShowItem()用法及代碼示例
- wxPython wx.RadioBox IsItemEnabled()用法及代碼示例
- wxPython wx.TreeCtrl CollapseAll()用法及代碼示例
- wxPython wx.ToolBar AddControl()用法及代碼示例
- wxPython wx.TreeCtrl CollapseAllChildren()用法及代碼示例
- wxPython wx.TreeCtrl ClearFocusedItem()用法及代碼示例
- wxPython wx.TreeCtrl CollapseAndReset()用法及代碼示例
- wxPython wx.TreeCtrl AddRoot()用法及代碼示例
- wxPython wx.ToolBar AddSeparator()用法及代碼示例
- wxPython wx.RadioBOx SetItemHelpText()用法及代碼示例
- wxPython wx.RadioBOx IsItemShown()用法及代碼示例
- wxPython wx.RadioBox SetItemLabel()用法及代碼示例
- wxPython wx.TreeCtrl AssignImageList()用法及代碼示例
- wxPython wx.StaticBox Enable()用法及代碼示例
- wxPython wx.TreeCtrl AppendItem()用法及代碼示例
注:本文由純淨天空篩選整理自RahulSabharwal大神的英文原創作品 wxPython – Delete() method in wx.TreeCtrl。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。