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
#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
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
Quote
#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?
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [Tkinter] Not getting entry values on button click & treeview not updating ? swanysto 4 271 May-10-2019, 04:16 PM
Last Post: swanysto
  [Tkinter] Image does not show in treeview. KevinBrown 3 191 May-05-2019, 11:47 PM
Last Post: KevinBrown
  [Tkinter] TKINTER quiz using sqlite3 database hezza_23 44 1,274 Apr-15-2019, 11:46 AM
Last Post: hezza_23
  [Tkinter] Tkinter timetabl using treeview mntfr 3 230 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 499 Jan-23-2019, 08:45 AM
Last Post: Larz60+
  [Tkinter] Treeview automatically adjust it's size when pack inside frame Prince_Bhatia 1 3,175 Jul-25-2018, 03:24 AM
Last Post: Larz60+
  bind hover on tkinter.ttk.Treeview Larz60+ 4 4,977 May-20-2017, 10:28 AM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)