【问题标题】:Programatically collapse branches in a Tix hlist checklist?以编程方式折叠 Tix hlist 清单中的分支?
【发布时间】:2018-07-11 23:00:43
【问题描述】:

对于我在 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 应该可用,对吧?

【问题讨论】:

    标签: python tix


    【解决方案1】:

    我终于找到了一种方法来让它工作。我完全放弃了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"])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-17
      • 2016-03-22
      • 2012-07-01
      • 1970-01-01
      • 2018-10-29
      • 1970-01-01
      相关资源
      最近更新 更多