Expand/collapse ttk Treeview branch

throwaway17434 picture throwaway17434 · Dec 2, 2013 · Viewed 7.3k times · Source

I would like to know the command for collapsing and expanding a branch in ttk.Treeview.

Here is a minimalistic example code:

#! coding=utf-8
import tkinter as tk
from tkinter import ttk

root = tk.Tk()
tree = ttk.Treeview(root)
tree.pack(fill=tk.BOTH,expand=True)

tree.insert("", index="end",iid="Main", text="main branch")
tree.insert("Main", index="end", text="Stuff 1")
tree.insert("Main", index="end", text="Stuff 2")

root.mainloop()

What command opens and/or expands the "main branch"? There has to be one, since these are called when clicking the plus and minus signs.

Answer

Todd picture Todd · Dec 2, 2013

For me (Win 7, Py2.7), your example comes up with the branch closed, but you can open or close it as you like with this command:

tree.item("Main", open=False)

Set it to False to close it.

See these topics:

25.2. tkinter.ttk - Tk themed widgets - Item options

25.2. tkinter.ttk - Tk themed widgets - item method

Item options can be set either with insert(), or after the fact with item().