Я пытаюсь отобразить простой Python словарь в виджете tkinter Treeview
. Я адаптировал некоторый код, который нашел здесь, для своих целей, но я не могу заставить код отображать больше, чем первое слово каждого из значений в списках строк в словаре.
Полагаю, что что-то в том, как настроены столбцы древовидной структуры, ограничивает отображение одним словом, но я не могу определить правильный флаг для настройки. Я оставил несколько неудачных попыток в коде как закомментированные строки.
# Credit to https://stackoverflow.com/a/22722889/122364
import uuid
import tkinter as tk
from tkinter import ttk, filedialog
import os
#Code is working OK other than clipping of each string in dictionary to first word.
TestData = {
"DATE": [
"1916",
"1935",
"2019",
"2020",
"29 January 2016",
"31st of March",
"3rd day of March",
"5 June 2019",
"5/6/2019",
"5th June 2019",
"March",
"March 3rd, 2015"
],
"ORG": [
"Merril Lynch",
"Goldman Sachs"
],
"TIME": [],
}
def Convert(lst):
res_dct = {i: lst[i] for i in range(0, len(lst))}
return res_dct
def json_tree(tree, parent, dictionary):
for key in dictionary:
uid = uuid.uuid4()
if isinstance(dictionary[key], dict):
tree.insert(parent, 'end', uid, text=dictionary[key])
## tree.insert(parent, 'end', uid, text=key)
json_tree(tree, uid, dictionary[key])
elif isinstance(dictionary[key], list):
## print("list found")
print(dictionary[key])
tree.insert(parent, 'end', uid, text=key)
json_tree(tree,
uid,
## dict(i for i in enumerate(tuple(dictionary[key]))))
Convert(dictionary[key]))
## dict([(i, x) for i, x in enumerate(dictionary[key])]))
elif isinstance(dictionary[key], tuple):
tree.insert(parent, 'end', uid, text=key + '[]')
json_tree(tree,
uid,
dict([(i, x) for i, x in enumerate(dictionary[key])]))
else:
value = dictionary[key]
if value is None:
value = 'None'
tree.insert(parent, 'end', uid, text=key, value=value)
def show_data(data):
# Setup the root UI
root = tk.Tk()
root.title("JSON viewer")
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
# Setup the Frames
tree_frame = ttk.Frame(root, padding="3")
tree_frame.grid(row=0, column=0, sticky=tk.NSEW)
# Setup the Tree
tree = ttk.Treeview(tree_frame, columns='Values', selectmode="extended")
tree.column('Values', width=100, anchor='center')
tree.heading('Values', text='Values')
json_tree(tree, '', data)
tree.pack(fill=tk.BOTH, expand=1)
# Limit windows minimum dimensions
root.update_idletasks()
root.minsize(800, 800)
root.mainloop()
if __name__ == '__main__':
show_data(TestData)