Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wxPython TreeCtrl very slow with millions of nodes (on multi-select tree control)

I am creating a tree with millions of nodes, but when I switched to using multiple-select on a tree control (wx.TR_MULTIPLE), actions on tree become slower, I only click to select a node and it takes me a few seconds. This does not happen when I use the single-select style (wx.TR_SINGLE).

I have tried to not set data for any node and did not use any event but it still slow. Is there any way to use multiple-select on a tree control and the tree still fast as single-select?

I've pasted the modified demo code in below:

import wx


class MyTree(wx.TreeCtrl):
    def __init__(self, parent, id, pos, size, style):
        wx.TreeCtrl.__init__(self, parent, id, pos, size, style)
        self.Bind(wx.EVT_TREE_SEL_CHANGED, self.item_changed)

    def item_changed(self, evt):
        print(self.GetItemData(evt.GetItem()))


class TreePanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.tree = MyTree(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TR_HAS_BUTTONS | wx.TR_MULTIPLE)
        self.root = self.tree.AddRoot('ROOT')
        node1 = self.tree.InsertItem(self.root, 0, 'Node 1', data='node 1')
        for i in range(1000000):
            self.tree.PrependItem(node1, 'Sub node 1: ' + str(i), data='Sub node 1: ' + str(i))
        self.tree.Expand(self.root)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.tree, 1, 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()
like image 792
Thiện Nhơn Avatar asked Aug 20 '19 04:08

Thiện Nhơn


1 Answers

I also have the same problems. But when I change to the single choice it becomes fast again, but at this time we can not choose multiple node... hmmm. I think this related with C-code inside the framework

like image 158
Million Smile Avatar answered Nov 16 '22 01:11

Million Smile