0

我有一个 SQLite 数据库,我要从中读取并将其中一个表数据列出到树视图中。为了让它发挥作用,我一直在寻找很长时间,我正在努力寻找任何对我有用或有意义的东西。例如,在我的表格中,我有标题“会员 ID”和“全名”。

出于测试目的,我创建了存储这些值的字符串的变量。

root = Tk()

name = "cameron"
id="223344"

lblsearchName = Label(root, text="Full Name:")
lblsearchName.grid(sticky=E)
searchEntry = Entry(root)
searchEntry.grid(column=1, sticky=E)

treeView = ttk.Treeview(root)
treeView.grid(columnspan=2)

root.mainloop()

如何根据我的数据库表中的标题在树视图中创建标题?我现在如何读取数据库,但我需要知道如何将这些值插入到树视图中。(对于这个例子'name'和'id')

4

1 回答 1

4
# set up the columns and headings
# In reality "Member ID" would be exported from the database
treeview["columns"] = ["Member ID", "Full Name"]
treeview["show"] = "headings"
treeview.heading("Member ID", text="Member ID")
treeview.heading("Full Name", text="Full Name")

# Add content using (where index is the position/row of the treeview)
# iid is the item index (used to access a specific element in the treeview)
# you can set iid to be equal to the index
tuples = [(1, "Name1"),(2, "Name2")]
index = iid = 0
for row in tuples:
    treeView.insert("", index, iid, values=row)
    index = iid = index + 1

样本输出:

显示输出代码创建的屏幕截图

有关标题的更多信息。

有关插入的更多信息。

有关选项的更多信息(例如列和标题)

于 2017-11-27T16:09:01.753 回答