首页 > 解决方案 > Python kivy 在 TreeView 中显示大量数据

问题描述

我在使用 Kivy TreeView 的 Python 中遇到了优化问题。我有大量数据,我想使用 TreeView 在 kivy 中显示近 16 万个项目。

树的结构是这样的:Root -> Groups -> Types -> Items. 只有物品是大量的。组 + 类型少于 200。

当加载和显示所有160k项目时它是不可用的,所以我做了这个,当你点击 TreeView 中的类型时,与该类型对应的项目被加载并显示。它减少了少于几百个项目的类型的时间。当我单击包含多达 8k 个项目的类型时,加载和显示它们的速度非常慢。

我正在寻找一种更好的方法,最好使用 TreeView 和 Kivy,但对替代方案持开放态度。我需要补充一点,我想使用一些 GUI 对这些项目进行操作。

我正在使用的自定义 TreeViewLabel 类:

class TreeViewFile(TreeViewLabel):

    def __init__(self, big_path=None, name=None, files_info=None, root=None, **kwargs):
        super(TreeViewFile, self).__init__(**kwargs)
        self._big_path = big_path
        self._files_info = files_info
        self.text = name
        self._root = root

    # On clik, if leaf create TreeViewItem
    def on_touch_down(self, touch):
        if not self.is_leaf:
            self._root.toggle_node(self)
        elif self._files_info is not None:
            for file in self._files_info.keys():
                self._root.add_node(TreeViewItem(file_info=self._files_info[file]), self)

编辑

查看评论我更改了我的代码,所以现在它在 TreeView 中使用 RecycleView。加载速度变快了,但问题在于,RecycleView 可以在 TreeView 内滚动。它看起来很奇怪,但有效。

def on_touch_down(self, touch):
        if not self.is_leaf:
            self._root.toggle_node(self)
        elif self._files_info is not None:
            rv = RV() # RecycleView
            data = []
            for file in self._files_info.keys():
                if isinstance(self._files_info[file][-1], str):
                    data.append(self._files_info[file][-1])
                else:
                    data.append(self._files_info[file][0])
            rv.data = [{'text': x} for x in data]
            self._root.add_node(rv, self)

标签: pythonoptimizationtreeviewkivy

解决方案


推荐阅读