The Python Oracle

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

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: The World Wide Mind

--

Chapters
00:00 Question
02:40 Accepted answer (Score 0)
03:12 Answer 2 (Score 4)
04:02 Thank you

--

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

Answer 1 links:
[Building a hierarchical treeview]: https://www.lynda.com/Tkinter-tutorials/...

--

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