tkinter - 将带有字典的嵌套列表中的数据添加到树视图 [英] tkinter - add data from nested list with dictionary to treeview

查看:19
本文介绍了tkinter - 将带有字典的嵌套列表中的数据添加到树视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些数据放入我的树视图中,而我是树视图的新手,并且如果尝试阅读文档,我会完全理解它,但会更加困惑.

I am trying to put some data in my treeview and I'm new to the treeview and do'sent understand it fully, if tried to read the documentation, but got even more confused.

我创建了一个包含 2 个子列表和最后一些字典的嵌套列表.

I created a nested list with 2 sublists and last some dictionaries.

isolering = [
[
{"name": "mineraluld"},
{"dim": "0,195"},
{"lambda": "0,37"},
{"z": "250"},
{"fire": "NA"}
],
[
{"name": "mineraluld2"},
{"dim": "0,195"},
{"lambda": "0,37"},
{"z": "250"},
{"fire": "NA"}
]]

materialLibrary = [isolering]

现在我想不出将数据放入树状视图的正确方法.

Now i can't figure out the proper way to put my data in the treeview.

这就是我得到的程度.我想不通,调用我的数据的方式.我试图这样做,就像你用它的索引来称呼它一样.但我明白这是错误的.

This is how far I got. I can't figure out, the way to call my data. I trying to it, like you would call it by it's index. But I understand it's wrong.

tree.insert("" , 0, text="Name")
tree.insert("", 1, "dirIso", text="Isolering")
tree.insert("dirIso", 1, text=materialLibrary[0][1][0]["name"],values=(materialLibrary[0][1][0]["dim"],
                                                                     materialLibrary[0][1][0]["lambda"],
                                                                     materialLibrary[0][1][0]["z"],
                                                                     materialLibrary[0][1][0]["fire"]))

这里有一张我正在尝试完成的帽子图片.

I have here a picture of hat I'm trying to accomplish.

我收到的错误信息是这样的:KeyError: 'dim'

The error message I receive is this: KeyError: 'dim'

感谢任何帮助或指出正确的方向.

Any help is appreciated or point in the right direction.

谢谢

推荐答案

问题:将带有字典的嵌套列表中的数据添加到树视图

Question: add data from nested list with dictionary to treeview

# Set 'text' to the first column heading
tree.heading('#0', text='Name')

# Insert Tree Heading as Item 'dirIso'
# Set 'text' to "Isolering"
tree.insert("", 1, "dirIso", text="Isolering")

# Loop first list
for n, dirIso in enumerate(isolering,1):
    # Make a list of values from the list of Dictionaries
    list_of_column_values = 
        [list(_dict.values())[0] for _dict in dirIso]

    # Insert the list of values
    # First value goes to Treeview 'text'
    # All other values into the following Columns
    tree.insert('dirIso', n, text=list_of_column_values[0], 
                             values=list_of_column_values[1:])

用 Python 测试:3.5

Tested with Python: 3.5

这篇关于tkinter - 将带有字典的嵌套列表中的数据添加到树视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆