Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Get data from tablelist by "$w get 0 end"

26 views
Skip to first unread message

Alexandru

unread,
Mar 30, 2017, 4:50:08 AM3/30/17
to
Hi,

I'm just wondering if "$w get 0 end" should always return a list of lists for a multi column tablelist widget. Same question for the case where I want to get only the selected rows with "$w get $idxs", where "idxs" is a list of indices.

For example, if the tablelist contains at least two rows, if returns a list of lists. If the table contains only one row, it return a list with so many elements as the number of columns. So it's not possible to say from looking at the return value, how many rows the table list has. I need to inquire this value separately and then define an if/else.

This is my code

## Copy seleted rows from a tablelist widget table to clipboard.
# If nothing is selected, all rows are copied.
# If some rows are selected, only those rows will be copied.
# \param w the path of the tablelist.
# \return
proc TableCopy {w} {
# Get indices of selected rows
set idxs [$w curselection]
set nrows [llength $idxs]
if {[llength $idxs]==0} {
set data [$w get 0 end]
} else {
set data [$w get $idxs]
# If there is just one row selected, then this is not visible by looking at "data" variable
if {[llength $idxs]==1} {
# Put data into a list so that it's clear that it contains only one row
set data [list $data]
}
}
# Put table on the clipboard
ClipboardSetTable $data
}

Originally, my code contained no "if {[llength $idxs]==1} {" an I discovered the bug not until today. The code still has a bug in the row "set data [$w get 0 end]" where I still need an additional "if", for the case nothing is selected and the table contains only one row.

Cheers
Alexandru

nemethi

unread,
Mar 30, 2017, 9:50:09 AM3/30/17
to
"$w get 0 end" *does* always return a list of items, which is a list of
lists (see the reference manual). This is valid also if the tablelist
contains only one row (in this case the result list has exactly one
element, which is the only item of the tablelist). Hence your code
doesn't need an additional "if" at that place!

"$w get $idxs" returns a list of items or just one item, depending on
the number of indices in $idxs. This is the documented behavior, which
has the minor drawback that one might need an "if {[llength $idxs] ==
1}" check. On the other hand, this rule comes in handy if you want to
get exactly one item, say, for index = 10, because in that case you can
just write "set item [$w get 10]".

--
Csaba Nemethi http://www.nemethi.de mailto:csaba....@t-online.de

Alexandru

unread,
Mar 30, 2017, 10:18:15 AM3/30/17
to
Thanks for the explanations. I understand that "$w get 0 end" always returns a list of lists, while "$w get $idxs" only if $idxs contains more than one index.

I still think that "$w get 10" should return a list of one sub-list. On the other side, changing this would be a big compatibility issue so I'll have to live with that.

Thanks again.

Regards
Alexandru
0 new messages