Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expand/collapse ttk Treeview branch

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.

like image 839
throwaway17434 Avatar asked Dec 02 '13 14:12

throwaway17434


1 Answers

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().

like image 106
Todd Avatar answered Nov 07 '22 23:11

Todd