Bottom Page

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
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')), y=0)

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

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

        # search button = Button(master, text="Search", width=12, height=1, bg='green', command=self.search_db), y=102)
        #report button = Button(master, text="View Report", width=12, height=1, bg='green', command = self.new_window), 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.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')), y=140)

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

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

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

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

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

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

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

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

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

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

        self.ent6 = Entry(self.master, width=30), 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), y=380)

        # button to delete
        self.delete = Button(self.master, text="Delete", width=20, height=2, bg='red', command=self.delete_db), 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(),))
        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(),))
        tkinter.messagebox.showinfo("Success", "Deleted Successfully")
    def new_window(self):
        win2 = Toplevel()
        message = "Weekly Report"
        Label(win2, text=message).pack()
        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)
root = Tk()
b = Application(root)
root.resizable(False, False)
Any help would be appriciated
Larz60+ wrote Jan-12-2019, 10:21 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

I added tags for you this post. Please use in future posts. Thank You
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?

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [PyQt] How to populate a treeview on a GUI with a dictionary mart79 1 131 Aug-05-2019, 01:30 PM
Last Post: Denni
  [Tkinter] Not getting entry values on button click & treeview not updating ? swanysto 4 502 May-10-2019, 04:16 PM
Last Post: swanysto
  [Tkinter] Image does not show in treeview. KevinBrown 3 317 May-05-2019, 11:47 PM
Last Post: KevinBrown
  [Tkinter] TKINTER quiz using sqlite3 database hezza_23 44 2,060 Apr-15-2019, 11:46 AM
Last Post: hezza_23
  [Tkinter] Tkinter timetabl using treeview mntfr 3 316 Feb-05-2019, 09:36 PM
Last Post: Larz60+
  sQlite3 output to tkinter treeview - how do I set / increase width of the output? dewijones67 5 677 Jan-23-2019, 08:45 AM
Last Post: Larz60+
  [Tkinter] Treeview automatically adjust it's size when pack inside frame Prince_Bhatia 1 4,156 Jul-25-2018, 03:24 AM
Last Post: Larz60+
  bind hover on tkinter.ttk.Treeview Larz60+ 4 5,516 May-20-2017, 10:28 AM
Last Post: Larz60+

Forum Jump:

Users browsing this thread: 1 Guest(s)