简体   繁体   English

如何在Tkinter TreeView中输出sqlite3数据

[英]How to output sqlite3 data in tkinter treeview

Am having issue with how output all records in my sqlite3 db into tkinter treeview .It outputs only the last record in the db and also the records doesn't appear at the column specified for it.The print method print all the db records to my terminal but doesn't output all the records to the treeview widget. 上午有有在我的所有记录如何输出问题sqlite3分贝到tkinter treeview 。它仅输出在最后一个记录db ,并记录不会出现在对后援指定的列print方法印刷的所有db记录我的终端,但不会将所有记录输出到treeview小部件。

Your suggestions are welcome to achieve this 欢迎您提出建议以实现这一目标

from tkinter import ttk
import tkinter as tk
import sqlite3


def connect():
    conn = sqlite3.connect("TRIAL.db")
    cur = conn.cursor()
    cur.execute("CREATE TABLE IF NOT EXISTS profile(id INTEGER PRIMARY KEY, 
First TEXT, Surname TEXT)")
    conn.commit()
    conn.close()


def View():
    conn = sqlite3.connect("TRIAL.db")
    cur = conn.cursor()
    cur.execute("SELECT * FROM profile")
    rows = cur.fetchall()
    for row in rows:
        print(row) # it print all records in the database
    tree.insert("", tk.END, values=row)
    conn.close()


connect()  #  this to create the db

root = tk.Tk()
root.geometry("400x400")

tree= ttk.Treeview(root, column=("column", "colunn1"))
tree.heading("#0", text="NUMBER")
tree.heading("#1", text="FIRST NAME")
tree.heading("#2", text="SURNAME")
tree.pack()

b2 = tk.Button(text="view data", command=View)
b2.pack()

root.mainloop()

There are two issues in your code: 您的代码中有两个问题:

  • The first is that tree.insert("", tk.END, values=row) is not inside the for loop, so only the last row will be inserted in the treeview. 第一个是tree.insert("", tk.END, values=row)不在for循环内,因此仅最后一行将插入到树视图中。
  • The second is that the column #0 is a special column, and its value is set with text=... , not with values=(...) . 第二个是#0列是一个特殊列,其值是使用text=...设置的,而不是使用values=(...) To display correctly your data, you have at least two alternatives: 为了正确显示数据,您至少有两种选择:

    • The first is to replace tree.insert("", tk.END, values=row) by tree.insert("", tk.END, text=row[0], values=row[1:]) 第一种是将tree.insert("", tk.END, values=row) tree.insert("", tk.END, text=row[0], values=row[1:])
    • The second is not to use the special column #0 , you don't need it because you are using the treeview as a table. 第二个是不使用特殊列#0 ,因为树视图用作表,所以您不需要它。 When you create the tree, use the option show='headings' to hide column #0 and create 3 columns: 创建树时,使用选项show='headings'隐藏#0列并创建3列:

       from tkinter import ttk import tkinter as tk import sqlite3 def connect(): conn = sqlite3.connect("TRIAL.db") cur = conn.cursor() cur.execute("CREATE TABLE IF NOT EXISTS profile(id INTEGER PRIMARY KEY, First TEXT, Surname TEXT)") conn.commit() conn.close() def View(): conn = sqlite3.connect("TRIAL.db") cur = conn.cursor() cur.execute("SELECT * FROM profile") rows = cur.fetchall() for row in rows: print(row) # it print all records in the database tree.insert("", tk.END, values=row) conn.close() connect() # this to create the db root = tk.Tk() root.geometry("400x400") tree= ttk.Treeview(root, column=("column1", "column2", "column3"), show='headings') tree.heading("#1", text="NUMBER") tree.heading("#2", text="FIRST NAME") tree.heading("#3", text="SURNAME") tree.pack() b2 = tk.Button(text="view data", command=View) b2.pack() root.mainloop() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM