views:

257

answers:

2

Hello:

I am using the following procedure to delete a record within a database that is displayed within a treeview widget (z1):

set z1 [ttk::treeview .c1.t1 -columns {first last} -show headings]

proc Dlt {} {
    global z1 z11
    sqlite3 db test.db
    db eval {
        DELETE From t1 Where First_Name = $z11 and Last_Name = $z11
    }
    db close
}

$z11 in the sql statement should be the treeview selection. Unfortunately, I cannot figure out how to set a variable to equal the treeview selection. I can set a variable to equal the index, which is: set z11 [$z1 index [$z1 selection]]. This will give me the index of the treeview selection; however, I am trying to get the string value of the treeview selection.

Does anyone know what the correct syntax is to set a variable to equal a treeview selection?

Thank you,

+1  A: 

To get the values for an item in the tree you would use the item subcommand of the tree. For example:

set selection [.tree selection]
set text [.tree item $selection -text]

This is all documented on the man page for the treeview widget.

Bryan Oakley
Thanks Bryan - (I would vote you up, but I do not have enough rep points) I tried your example and replaced $z11 with $text in the sql statement, but it did not work. I think I will have to make a label have the text $text to see what is showing up in the variable $text. Thanks for the link. I tried using the tcl resources in the past; but, without examples, it's a little confusing, since I am fairly new to this language.
Without knowing what you mean by "did not work" it's hard to give any more advice. It's been a while since I used sqlite but the above looks ok to me.
Bryan Oakley
I clicked on one of the records in the treeview, and then pressed the button that fires the Dlt procedure; however, the record did not delete form the database. Just to make sure the Dlt procedure works, I created 2 entry boxes, set 2 textvariables, and put them into the sql statement. When I type the exact text of the record within the tree and press the Dlt proc, the record gets deleted. I am sure your solution is right, I'll just need to keep working at it.Thanks,
This worked:set selection [.tree. selection]set text [.tree item $selection -values]
A: 

As an aside, what platform are you working with? If Windows, for debugging purposes, you can add a "console show" command to your code to display an interactive console window. With that open, you can just use [puts] to display variable values. So, you could just use "puts $text" (from within your code) to see the value of your text variable.

Also, you can just type commands directly into the console for immediate evaluation. In many situations, spending a few minutes in the console can be very enlightening.

If you're not running under Windows, you don't even need the "console show" command, as anything written to stdout should appear in the original shell window.

Jeff Godfrey