mercredi 29 janvier 2020

Trying to parse a CheckboxTreeview from ttkwidgets and get the checkbox status of all the items in the tree

I've been trying to parse a checkbox treeview to return a dictionary with the item's ID as the key and the checkbox status as the value as "checked", "unchecked" and "tristate". However, I tried to sort the items by using CheckboxTreview.get_checked(). I don't know if i'm using it wrong or this is just a flaw from the package but it only returns the checked item from the highest level ('' as the parent).

from tkinter import *
from tkinter import ttk
from ttkwidgets import CheckboxTreeview

def parse_Tree(tree, parent):

    children = list(tree.get_children(parent))
    checkedList = tree.get_checked()
    itemDic = {}
    #print(checkedList)

    for item in children:
        if tree.get_children(item) == () and item in checkedList:
            itemDic[item] = "checked"
        elif tree.get_children(item) != () and item in checkedList:
            itemDic[item] = "checked"
            itemDic.update(parse_Tree(tree, item))
        elif tree.get_children(item) != () and item not in checkedList:
            for boxStatus in parse_Tree(tree, item).values():
                if boxStatus == "checked" or boxStatus == "tristate":
                    itemDic[item] = "tristate"
                else:
                    itemDic[item] = "unchecked"
                    itemDic.update(parse_Tree(tree,item))
        else:
            itemDic[item] = "unchecked"

    return itemDic

def listTreeview(textFile):

    list = []
    file = open(textFile, "r")
    treeview = file.read().split("\n")
    file.close()
    for item in treeview:
        list += [item.split(",")]
    root = Tk()
    master = ''
    level = []
    tree = CheckboxTreeview(root)
    for index,i in enumerate(list):
        indent = 0

        while i[0][indent] == ' ': indent += 1

        if indent%4:
            print("wrong indentation")
            break
        else:
            i[0] = i[0].replace(' ','')

        level.append(int(indent/4))

        if len(level)==1:
            tree.insert(master,'end',i[0], text = i[0])
        elif level[index]-level[index-1] == 1:
            master = list[index - 1][0]
            tree.insert(master, 'end', i[0], text=i[0])
        elif level[index]-level[index-1] < 0:
            prev = index-1
            while level[index] != level[prev]:
                prev -= 1
            master = tree.parent(list[prev][0])
            tree.insert(master,'end',i[0], text = i[0])
        elif level[index] - level[index - 1] > 1:
            print('wrong indentation')
        else: #level hasnt change
            tree.insert(master, 'end', i[0], text=i[0])
        if i[1] == '1':
            tree.change_state(i[0], "checked")


    tree.expand_all()
    dic = parse_Tree(tree,'')
    print(dic)
    tree.pack()
    root.mainloop()

listTreeview("Treeview.txt")

I parsed the following text file to work with, the indentation indicates the level and the last number indicates if it is checked or not.In this situation for exemple, item4.1.1 should appear as item4.1.1:"checked" but it doesn't...

Is there any other way to go through a checkbox treeview and get the state of each items?

item0,1
item1,0
    item1.1,0
    item1.2,0
item2,0
    item2.1,1
    item2.2,0
        item2.2.1,1
        item2.2.2,0
            item2.2.2.1,0
    item2.3,0
        item2.3.1,1
item3,1
item4,0
    item4.1,1
    item4.2,0
    item4.2.1,0



Aucun commentaire:

Enregistrer un commentaire