以编程方式折叠 Tix hlist 清单中的分支?

Programatically collapse branches in a Tix hlist checklist?

对于我在 Python 中的第一个 GUI,我使用 Tix 作为其内置的 'checklist' 和 'hlist' 小部件来构建一个带有复选框的树视图树的每个分支和叶子。它主要运作良好。但是我一直无法弄清楚的一件事是如何以编程方式折叠树的分支。

我希望在首次显示清单时折叠一些分支,并且能够有一个 "Collapse All" 按钮和一个 "Expand All" 按钮。

下面是我的代码的清单部分。我希望 checkList.close(i["id"]) 行可能会关闭分支,但事实并非如此。

任何人都可以教我以编程方式折叠 Tix checklist/hlist 树中的分支的正确方法吗?

checkList = Tix.CheckList(win)
checkList.pack(in_=frameTop, side=Tix.LEFT, fill=Tix.BOTH, expand=Tix.YES)
checkList.hlist.config(bg='white', selectbackground='white', selectforeground='black', header=True, browsecmd=itemEvent)
checkList.hlist.header_create(0, itemtype=Tix.TEXT, text='Select layers to add to the map', relief='flat')
checkList.hlist.bind("<ButtonRelease-1>", checkListClicked)

for i in items:
    try:
        checkList.hlist.add(i["id"], text=i["text"])
    except:
        print "WARNING:  Failed to add item to checklist:  {} - {}".format(i["id"], text=i["text"])
    checkList.close(i["id"])
    checkList.setstatus(i["id"], "off")

checkList.autosetmode()

我希望下面的代码能够工作(例如,在上面的 for 循环中):

checkList.setmode(i["id"], "close")
checkList.close(i["id"])

但它给了我错误,AttributeError: setmode。这很奇怪,因为据我所知,setmode 应该可用,对吧?

我终于想出了一个方法来让它工作。我完全放弃了 setmode,因为它似乎不适用于我的 Python 环境;然后因为我已经在那里得到 autosetmode,所以我可以在 autosetmode 有 运行.

之后使用 close 命令

也就是说我要运行循环两次,感觉有点浪费,不过也没什么大不了的,至少现在可以得到我需要的结果了。

这是最终对我有用的方法(如下)。在这种情况下,我为每个具有父项的项目将分支设置为关闭(即,顶级项目打开,所有其他项目关闭)。

checkList = Tix.CheckList(win)
checkList.pack(in_=frameTop, side=Tix.LEFT, fill=Tix.BOTH, expand=Tix.YES)
checkList.hlist.config(bg='white', selectbackground='white', selectforeground='black', header=True, browsecmd=itemEvent)
checkList.hlist.header_create(0, itemtype=Tix.TEXT, text='Select layers to add to the map', relief='flat')
checkList.hlist.bind("<ButtonRelease-1>", checkListClicked)

for i in items:
    try:
        checkList.hlist.add(i["id"], text=i["text"])
    except:
        print "WARNING:  Failed to add item to checklist:  {} - {}".format(i["id"], text=i["text"])
    # Delete this next line
    #checkList.close(i["id"])
    checkList.setstatus(i["id"], "off")

checkList.autosetmode()
#  Add these following lines (include whatever condition you like in the 'if')
for i in items:
    if checkList.hlist.info_parent(i["id"]):
        checkList.close(i["id"])