0

For my first GUI in Python, I'm using Tix for its built-in 'checklist' and 'hlist' widgets to construct a tree view with checkboxes against each branch and leaf of the tree. It is mostly working well. But the one thing that I have not been able to figure out is how to programmatically collapse branches of the tree.

I would like to have some branches collapsed when the checklist is first displayed and to be able to have a button to "Collapse All" and a button to "Expand All".

Below is the checklist portion of my code. I was hoping that the checkList.close(i["id"]) line might close the branch, but it does not.

Can anybody educate me on the correct way to programmatically collapse a branch in a Tix checklist/hlist tree ?

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

I would have expected the following to work (eg, within the above for loop):

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

But it gives me the error, AttributeError: setmode. Which is odd, because as far as I can tell, setmode should be available, right?

Son of a Beach
  • 1,733
  • 1
  • 11
  • 29

1 Answers1

0

I have figured out a way to get this working at last. I gave up on setmode completely as it doesn't seem to be available to my Python environment; then since I've already got autosetmode in there, I can use the close command AFTER the autosetmode has run.

It means I have to run through the loop twice, which feels a bit wasteful, but it's not a big deal, and at least I can now get the results I require.

Here is what finally worked for me (below). In this case, I'm setting the branch to closed for each item that has a parent (ie, the top-level items are open, and all others are closed).

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"])
Son of a Beach
  • 1,733
  • 1
  • 11
  • 29