How to get selection from treeview in Tcl

Viewed 43

I've got a simple GUI in TCL with treeview, idea is to read some folders and populate the treeview based on the contents of the folder.

I can create the structure for the treeview but I'm not sure how to get selected values when selected with mouse (single or multi-select).

Preference would be to have checkbox but reading up on the forum has made it clear that it is not possible with tree view.

I wonder if there any alternatives to checkbox?

Could I request some direction or advice on following:

  1. get all items selected below "Some Label", see Image attached.
  2. Or individually selected items e.g. A, B, XX, YY (see Image attached.)
  3. I found it impossible adding Y scroll bar, could someone advice.

TreeView GUI Image

below is a snippet of the code.

    set list0 {A B C D E F}
    set list1 {XX YY}


    ttk::treeview $tw.tv.tree -height 25 -columns "#0" -displaycolumns "#0" -selectmode extended
    $tw.tv.tree heading #0 -text "Name" -anchor center 
    $tw.tv.tree column #0 -anchor e -minwidth 200 -width 300 -stretch 1
    pack $tw.tv.tree
    
    $tw.tv.tree insert {} end -id Title1 -text "Some Label " 
    foreach key0 [lsort $list0] {
    puts "Key0: $key0"
    $tw.tv.tree insert Title1 end -text $key0 -tags clickable
    }
    $tw.tv.tree insert {} end -id Title2 -text "Some Other Label" 
    foreach key1 [lsort $list1] {
    puts "Key1: $key1"
    $tw.tv.tree insert Title2 end -text $key1 -tags clickable
    }

    grid columnconfigure $tw.tv 2 -weight 1
    grid rowconfigure $tw.tv 2 -weight 1
    

For selection I used one of the examples posted on a thread on this forum

    set selection [$tw.tv.tree selection]
    set text [$tw.tv.tree item $selection -text]
    puts "$text"

Unfortunately this returns an empty string.

ps: the code shown is only for treeview which is one part of the GUI, shown only to identify rookie error.

Thanks in advance,

George

0 Answers
Related