wx.TreeCtrl sort all items

Hi, I am trying to sort all items in a wx.TreeCtrl, but can’t seem to get it to work, heres my current code which is only sorting the parent items, but I want to sort all parent and all children items:

import wx

class TestFrame(wx.Dialog):

def init(self):

wx.Dialog.init(self, None, title=“tree”, size=(570, 700))

self.panel = wx.Panel(self, wx.ID_ANY)

self.tree = wx.TreeCtrl(self, pos=(30, 25), size=(505, 283))

root = self.tree.AddRoot(“Presets”)

nodes = [“A”,“Z”,“K”,“D”,“E”,“F”,“G”,“H”]

values = [“a”,“z”,“u”,“d”,“e”,“l”,“s”,“h”]

for s in nodes:

c = self.tree.AppendItem(root, s)

for t in values:

self.tree.AppendItem(c, t)

self.tree.Expand(root)

self.OnSortChildren(self)

def OnSortChildren(self, evt):

item = self.tree.GetSelection()

if item:

self.tree.SortChildren(item)

app = wx.App(redirect=False)

frame = TestFrame()

frame.Show()

app.MainLoop()

OK, got it working, this now sorts everything, this is my solution:

import wx

class TestFrame(wx.Dialog):

def init(self):

wx.Dialog.init(self, None, title=“tree”, size=(570, 700))

self.panel = wx.Panel(self, wx.ID_ANY)

self.tree = wx.TreeCtrl(self, pos=(30, 25), size=(505, 283))

root = self.tree.AddRoot(“Presets”)

nodes = [“A”,“Z”,“K”,“D”,“E”,“F”,“G”,“H”]

values = [“a”,“z”,“u”,“d”,“e”,“l”,“s”,“h”]

for s in nodes:

c = self.tree.AppendItem(root, s)

for t in values:

self.tree.AppendItem(c, t)

self.tree.Expand(root)

self.OnSortChildren(self)

def OnSortChildren(self, evt):

item = self.tree.GetRootItem()

if item:

self.tree.SortChildren(item)

child, cookie = self.tree.GetFirstChild(item)

while child.IsOk():

self.tree.SortChildren(child)

child, cookie = self.tree.GetNextChild(item, cookie)

app = wx.App(redirect=False)

frame = TestFrame()

frame.Show()

app.MainLoop()

···

On Friday, January 29, 2016 at 6:44:18 AM UTC, kevjwells wrote:

Hi, I am trying to sort all items in a wx.TreeCtrl, but can’t seem to get it to work, heres my current code which is only sorting the parent items, but I want to sort all parent and all children items:

import wx

class TestFrame(wx.Dialog):

def init(self):

wx.Dialog.init(self, None, title=“tree”, size=(570, 700))

self.panel = wx.Panel(self, wx.ID_ANY)

self.tree = wx.TreeCtrl(self, pos=(30, 25), size=(505, 283))

root = self.tree.AddRoot(“Presets”)

nodes = [“A”,“Z”,“K”,“D”,“E”,“F”,“G”,“H”]

values = [“a”,“z”,“u”,“d”,“e”,“l”,“s”,“h”]

for s in nodes:

c = self.tree.AppendItem(root, s)

for t in values:

self.tree.AppendItem(c, t)

self.tree.Expand(root)

self.OnSortChildren(self)

def OnSortChildren(self, evt):

item = self.tree.GetSelection()

if item:

self.tree.SortChildren(item)

app = wx.App(redirect=False)

frame = TestFrame()

frame.Show()

app.MainLoop()