使用 Sqlite 数据填充 Tcl Treeview

发布于 2024-08-05 05:56:33 字数 530 浏览 4 评论 0原文

我正在构建一个读取 Sqlite Db 的 Tcl 应用程序。目前,我可以使用 Tcl 前端将数据输入数据库。现在,我试图弄清楚如何从 Tcl 前端显示 Sqlite Db 中的数据。

经过一些研究,我发现树视图小部件可以很好地满足我的需求。我现在有以下代码:

set z1 [ttk::treeview .c1.t1 -columns {1 2} -show headings]
$z1 heading #1 -text "First Name"
$z1 heading #2 -text "Last Name"

proc Srch {} {global z1
    sqlite3 db test.db
    pack $z1
    db close
}

当执行“Srch”过程(按钮事件)时,树视图 (z1) 出现,标题为名字和姓氏。此外,Sqlite Db 已连接,然后关闭。

我想添加代码,在连接到数据库和打包树视图(z1)之间从 Sqlite 数据库填充树视图。有谁知道使用 Sqlite 数据填充 Tcl 树视图的正确语法?

I am building a Tcl application that reads off of a Sqlite Db. Currently, I can enter data into the database using the Tcl frontend. Now, I am trying to figure out how to display the data within the Sqlite Db from the Tcl frontend.

After a little bit of research, I found that the treeview widget would work well for my needs. I now have the following code:

set z1 [ttk::treeview .c1.t1 -columns {1 2} -show headings]
$z1 heading #1 -text "First Name"
$z1 heading #2 -text "Last Name"

proc Srch {} {global z1
    sqlite3 db test.db
    pack $z1
    db close
}

When the "Srch" procedure is executed (button event), the treeview (z1) appears with the headings First Name and Last Name. Additionally, the Sqlite Db gets connected, then closes.

I wanted to add code that would populate the treeview from the Sqlite Db between connecting to the Db and packing the treeview (z1). Does anyone know the correct syntax to populate a Tcl treeview with data from Sqlite?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮凉 2024-08-12 05:56:33

最好这样做:

proc Srch {} {
    global z1
    pack $z1
    foreach col {first last} name {First Last} {
        $z1 heading $col -text $name
    }
    sqlite3 db test.db
    db eval {SELECT first,last FROM t1} {
        $z1 insert {} end -values [list $first $last]
    }
    db close
}

这利用了 SQLite 与 Tcl 确实非常好的集成这一事实。

Better to do this:

proc Srch {} {
    global z1
    pack $z1
    foreach col {first last} name {First Last} {
        $z1 heading $col -text $name
    }
    sqlite3 db test.db
    db eval {SELECT first,last FROM t1} {
        $z1 insert {} end -values [list $first $last]
    }
    db close
}

This uses the fact that SQLite integrates very nicely with Tcl indeed.

优雅的叶子 2024-08-12 05:56:33

经过几个小时的反复试验,我终于弄清楚如何使用 sqlite 数据填充树视图小部件。不幸的是,我找不到任何网络资源,所以如果其他人遇到这个问题,答案如下:

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

proc Srch {} {
    global z1
    sqlite3 db test.db
    pack $z1
    set data [db eval {SELECT * FROM t1}]
    foreach col {first last} name {First Last} {
        .c1.t1 heading $col -text $name
    }
    foreach {first last} $data {
        .c1.t1 insert {} end -values [list $first $last]
    }
    db close
}

基本上,z1 是多列列表(树),Srch 是按钮驱动程序。执行时,树中将填充与每一列相对应的名字和姓氏。

After a few hours of trial and error, I finally figured out how to populate a treeview widget with sqlite data. Unfortunately, I could not find any web resources so in case anyone else has this problem, the answer is as follows:

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

proc Srch {} {
    global z1
    sqlite3 db test.db
    pack $z1
    set data [db eval {SELECT * FROM t1}]
    foreach col {first last} name {First Last} {
        .c1.t1 heading $col -text $name
    }
    foreach {first last} $data {
        .c1.t1 insert {} end -values [list $first $last]
    }
    db close
}

Basically, z1 is multi-column list (tree) and Srch is the button driven procedure. When executed the tree gets populated with first and last names, corresponding to each column.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文