将tkinter.TreeView连接到Python3中的数据层

时间:2017-12-03 21:54:52

标签: python-3.x tkinter treeview

我有一个树状结构的数据。可以描述为带有字典作为元素的嵌套列表。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from tkinter import *
from tkinter import ttk

root = Tk()

# for this example the "hidden" column is not hidden
tree = ttk.Treeview(root, columns=('datakey'))
tree.pack()

# the data (of a newsfeed reader aka newsaggregator)

theData = {
    "childs": [
        {
            "childs": [
                {
                    "childs": [
                        {
                            "htmlUrl": "http://www.news.org",
                            "text": "Newspaper",
                            "title": "Newspaper",
                            "type": "rss",
                            "xmlUrl": "http://www.news.org"
                        }
                    ],
                    "group": "News"
                },
                {
                    "htmlUrl": "https://www.theCinema.org",
                    "text": "Cinema",
                    "title": "Cinema",
                    "type": "atom",
                    "xmlUrl": "http://www.theCinema.org/newsfeed"
                }
            ],
            "group": "Group A"
        },
        {
            "htmlUrl": "http://www.top.org",
            "test": "Top Feed",
            "title": "Top Feed",
            "type": "rss",
            "xmlUrl": "http://www.top.org/feed"
        }
    ]
}

# populate the data into the tree
def _do_refresh_tree_level(data, parent_iid):
    nextParent_iid = parent_iid

    for key in data:
        if key == 'childs': # CHILDS
            # first create the group
            if 'group' in data.keys():
                nextParent_iid = tree.insert(parent=parent_iid,
                                             index=END,
                                             text=data['group'])
            # each child in the group
            idxCurChild = -1
            for child in data['childs']:
                idxCurChild += 1
                _do_refresh_tree_level(child, nextParent_iid)
        elif key == 'group': # GROUPS
            pass  # handled in CHILDS section
        else:  # FEED
            iid = tree.insert(parent_iid, index=END, text=data['title'])
            break

_do_refresh_tree_level(theData, '')

root.mainloop()

myData = [
            {
                "group": "A",
                "children": [
                    {
                        "name": "Tick"
                    },
                    {
                        "group": "B",
                        "children": [
                            {
                                "name": "Anna"
                            }
                        ]
                    }
                ]
            },
            {
                "name": "Bob"
            }
          ]

这些元素可以像这样贴身

theData['childs'][0]['group']  # "Group A"

是数据的结构和深度不固定。

现在我想将密钥(如RDBMS中的主键或OODBMS中的对象id)提供给数据结构中的所有元素。伪代码:

myIndex = {
        '100': ['childs'][0]['group'],
        ...
        }

这是不可能的,但解释了我的想法。这里的值可以描述为原始数据的地址。是否有一种pythonic方式来存储这些地址(而不是数据本身!)?

所以我想在下面的例子中创建一个连接列表(myIndex)来组合TreeView元素的id和有关在哪里找到原始数据的信息。

我可以将treeview-element-id直接存储到数据结构中(myData)。但这是IMO不优雅,并且在搜索数据树时搜索当前id(例如TreeView中当前所选项目)会浪费性能。

第二点是我的目标是将TreeView作为表示层中的GUI元素与myData分开作为数据在数据层

可能的解决方案?

这有效但是它是pythonic吗?

myIndex = {
    '100': ['childs'][0]['group']
    }

def getitem(data, idx_list):
    if len(idx_list) > 1:
        idx = idx_list[0]
        idx_list = idx_list[1:]
        return getitem(data.__getitem__(idx), idx_list)
    else:
        return data.__getitem__(idx_list[0])

getitem(theData, myIndex['100']) # "Group A"

也许我的问题的根源是数据本身的结构?但是我想使用JSON,因为它易于与Python一起使用,并且很容易为人类阅读。

0 个答案:

没有答案