0

对于我在 Python 中的第一个 GUI,我将Tix用于其内置的“checklist”和“hlist”小部件,以构建一个树视图,其中包含针对树的每个分支和叶子的复选框。它大部分运行良好。但我无法弄清楚的一件事是如何以编程方式折叠树的分支。

我希望在第一次显示清单时折叠一些分支,并且能够有一个“全部折叠”按钮和一个“全部展开”按钮。

以下是我的代码的清单部分。我希望这checkList.close(i["id"])条线可能会关闭分支,但它没有。

任何人都可以教育我以编程方式折叠 Tix 清单/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应该可用,对吧?

4

1 回答 1

0

我终于找到了一种方法来让它工作。我完全放弃了,setmode因为它似乎不适用于我的 Python 环境;那么既然我已经autosetmode进去了,我可以在运行close后使用命令autosetmode

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

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

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"])
于 2018-08-08T06:11:45.240 回答