The Python Oracle

Retrieving ttk.Treeview item's 'open' option as boolean

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Sunrise at the Stream

--

Chapters
00:00 Retrieving Ttk.Treeview Item'S 'Open' Option As Boolean
02:01 Accepted Answer Score 0
02:28 Answer 2 Score 5
03:06 Thank you

--

Full question
https://stackoverflow.com/questions/2053...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #treeview #tkinter #ttk

#avk47



ANSWER 1

Score 5


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.




ACCEPTED ANSWER

Score 0


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