Expand/collapse ttk Treeview branch

北慕城南 提交于 2020-02-22 04:21:14

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/20330139/expand-collapse-ttk-treeview-branch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!