Python Forum
[Tkinter] I need to know how to put my SQLite3 data into a Treeview
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] I need to know how to put my SQLite3 data into a Treeview
#1
Hi, I have to create a GUI that can retrieve stored information about students. I am pretty new to Python and Tkinter and very new to SQLite3. I am very very close, but I can not get my treeview to work. I have tried everything I can think of and have watched numerous videos online. I am wanting to display the stored data in a list-like format. The only thing I can think of is a treeview widget. Here is my code

from tkinter import *
from tkinter import ttk
import tkinter.messagebox 
import sqlite3


conn = sqlite3.connect('database.db')
c = conn.cursor()

class Application:
    def __init__(self, master):
        self.master = master
        # heading label
        self.heading = Label(master, text="Update Student Info",  fg='green', font=('broadway 40 bold'))
        self.heading.place(x=150, y=0)

        # search criteria -->name 
        self.name = Label(master, text="Enter Student's Name", font=('arial 18 bold'))
        self.name.place(x=0, y=60)

        # entry for  the name
        self.namenet = Entry(master, width=30)
        self.namenet.place(x=280, y=62)

        # search button
        self.search = Button(master, text="Search", width=12, height=1, bg='green', command=self.search_db)
        self.search.place(x=350, y=102)
        
        #report button
        self.search = Button(master, text="View Report", width=12, height=1, bg='green', command = self.new_window)
        self.search.place(x=900, y=102)
    # function to search
    def search_db(self):
        self.input = self.namenet.get()
        # execute sql 

        sql = "SELECT * FROM records WHERE name LIKE ?"
        self.res = c.execute(sql, (self.input,))
        for self.row in self.res:
            self.name1 = self.row[1]
            self.school = self.row[2]
            self.grade = self.row[3]
            self.checkoutdate = self.row[6]
            self.ebookname = self.row[4]
            self.bookcode = self.row[5]
        # creating the update form
        self.uname = Label(self.master, text="Student's Name", font=('arial 18 bold'))
        self.uname.place(x=0, y=140)

        self.uschool = Label(self.master, text="School", font=('arial 18 bold'))
        self.uschool.place(x=0, y=220)

        self.ugrade = Label(self.master, text="Grade", font=('arial 18 bold'))
        self.ugrade.place(x=0, y=180)

        self.ucheckoutdate = Label(self.master, text="Checkout Date", font=('arial 18 bold'))
        self.ucheckoutdate.place(x=0, y=340)

        self.uebookname = Label(self.master, text="eBook Name", font=('arial 18 bold'))
        self.uebookname.place(x=0, y=260)

        self.ubookcode = Label(self.master, text="eBook Code", font=('arial 18 bold'))
        self.ubookcode.place(x=0, y=300)

        # entries for each labels==========================================================
        # ===================filling the search result in the entry box to update
        self.ent1 = Entry(self.master, width=30)
        self.ent1.place(x=300, y=140)
        self.ent1.insert(END, str(self.name1))

        self.ent2 = Entry(self.master, width=30)
        self.ent2.place(x=300, y=180)
        self.ent2.insert(END, str(self.school))

        self.ent3 = Entry(self.master, width=30)
        self.ent3.place(x=300, y=220)
        self.ent3.insert(END, str(self.grade))

        self.ent4 = Entry(self.master, width=30)
        self.ent4.place(x=300, y=340)
        self.ent4.insert(END, str(self.checkoutdate))

        self.ent5 = Entry(self.master, width=30)
        self.ent5.place(x=300, y=260)
        self.ent5.insert(END, str(self.ebookname))

        self.ent6 = Entry(self.master, width=30)
        self.ent6.place(x=300, y=300)
        self.ent6.insert(END, str(self.bookcode))

        # button to execute update
        self.update = Button(self.master, text="Update", width=20, height=2, bg='lightblue', command=self.update_db)
        self.update.place(x=400, y=380)

        # button to delete
        self.delete = Button(self.master, text="Delete", width=20, height=2, bg='red', command=self.delete_db)
        self.delete.place(x=150, y=380)
    def update_db(self):
        # declaring the variables to update
        self.var1 = self.ent1.get() #updated name
        self.var2 = self.ent2.get() #updated school
        self.var3 = self.ent3.get() #updated grade
        self.var4 = self.ent4.get() #updated checkoutdate
        self.var5 = self.ent5.get() #updated bookcode
        self.var6 = self.ent6.get() #updated ebookname

        query = "UPDATE records SET name=?, grade=?, school=?, ebookname=?, bookcode=?, checkoutdate=? WHERE name LIKE ?"
        c.execute(query, (self.var1, self.var2, self.var3, self.var4, self.var5, self.var6, self.namenet.get(),))
        conn.commit()
        tkinter.messagebox.showinfo("Updated", "Successfully Updated.")
    def delete_db(self):
        # delete the appointment
        sql2 = "DELETE FROM records WHERE name LIKE ?"
        c.execute(sql2, (self.namenet.get(),))
        conn.commit()
        tkinter.messagebox.showinfo("Success", "Deleted Successfully")
        self.ent1.destroy()
        self.ent2.destroy()
        self.ent3.destroy()
        self.ent4.destroy()
        self.ent5.destroy()
        self.ent6.destroy()
        
    def new_window(self):
        win2 = Toplevel()
        message = "Weekly Report"
        Label(win2, text=message).pack()
        element_header=['1st','2nd','3rd','4th','5th','6th']
        treeScroll = ttk.Scrollbar(win2)
        treeScroll.pack(side=RIGHT, fill=Y)
        tree = ttk.Treeview(win2,columns=element_header,show = "headings", yscrollcommand = treeScroll)
        tree.heading("1st", text="Name")
        tree.heading("2nd", text="Grade")
        tree.heading("3rd", text="School")
        tree.heading("4th", text="Book Name")
        tree.heading("5th", text="Book Code")
        tree.heading("6th", text="Checkout Date")
        
        tree.pack(side=LEFT, fill=BOTH)
        treeScroll.config(command=tree.yview)
root = Tk()
b = Application(root)
root.geometry("1200x720+0+0")
root.resizable(False, False)
root.mainloop()
Any help would be appriciated
Reply
#2
Can't run here as there's no database, so you need to tell us if sqlite3 part is working OK.
I can help with the tkinter, but need some sample rows from database.
are you running on tkinter or Linux, and which flavor of OS?
I think a text widget with columns is probably the way to go.
One other question, do you need to access the data by clicking or anything else we should know about?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Search data in treeview without search button TomasSanchexx 3 1,529 Aug-12-2023, 03:17 AM
Last Post: deanhystad
  [Tkinter] How to insert data json to treeview tkinter? Shakanrose 8 4,211 Jan-19-2023, 03:58 PM
Last Post: Shakanrose
  PyQt5 form not displaying my data from SQLite3 Database Linuxdesire 2 4,927 Jan-10-2023, 09:51 PM
Last Post: gradlon93
Question [Tkinter] data enterred through gui is not storing in sqlite3 database Hilal 21 7,315 Dec-15-2021, 08:48 PM
Last Post: Hilal
  [Tkinter] load sqlite3 data into pdf rwahdan 5 4,253 Nov-29-2021, 07:58 PM
Last Post: Hilal
  [Tkinter] [split] Is there a way to embed a treeview as a row inside another treeview? CyKlop 5 3,303 Oct-20-2021, 12:14 AM
Last Post: CyKlop
  [Tkinter] acceleration of data output in treeview tkinter Vladimir1984 4 4,096 Nov-21-2020, 03:43 PM
Last Post: Vladimir1984
  sQlite3 output to tkinter treeview - how do I set / increase width of the output? dewijones67 5 6,575 Jan-23-2019, 08:45 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020