Skip to content Skip to sidebar Skip to footer

Retrieving Ttk.treeview Item's 'open' Option As Boolean

I encounter untoward behavior when querying the open option of a (Python) ttk.Treeview item. The visibility of a node (item) can be set by doing something like: tree.item(someItemI

Solution 1:

In the Lynda course by Barron Stone, Python GUI Development with Tkinter, a lesson video, Building a hierarchical treeview, has an example that shows how to get an "is open?" result. I have modified the example below:

Python 3.5 in IDLE console

>>>from tkinter import *>>>from tkinter import ttk>>>root = Tk()>>>treeview = ttk.Treeview(root)>>>treeview.pack()>>>treeview.insert('', '0', 'par1', text = 'Parent')
'par1'
>>>treeview.insert('par1', '0', 'child1', text = 'Child')
'child1'
>>>treeview.item('par1', 'open')
0
>>>treeview.item('par1', open = True)
{}
>>>treeview.item('par1', 'open')
1
>>>

Not a boolean as requested, but an int that is just as good.

Solution 2:

There was a tkinter option: tkinter.wantObjects that some people offered to change to False. It should make Tk to do not use TCL_objs. But when I tried it the TreeView looked broken.

As a workaround I used BooleanVar in way like this:

open_opt = BooleanVar()
for row in tree.get_children():
    open_opt.set(str(tree.item(row, option='open')))
    opened = open_opt.get()

This way seemed working to me

Post a Comment for "Retrieving Ttk.treeview Item's 'open' Option As Boolean"