'How do get to see my updated rows in the tree view after I update it in the database?
I am creating Project for my Office. I am using Python, Sqlite3, and Tkinter. I am able to display records from Sqlite3 through Tkinter but when I insert new records/data, it inserts into Sqlite3 but Tkinter does not display the new record inserted. Here is my code; I hope it is understandable.
Here I have added a button to find my Data from the database and view it on the screen. When I select any number of the Data and Update it according to my choice, it is being updated on the Database but It is not immediately updated in the Treeview. Please give me a solution for this. Will be a huge help!
from tkinter import *
from tkinter.ttk import Combobox
from tkinter import messagebox
import sqlite3
import tkinter as tk
from tkinter import ttk
r = tk.Tk()
r.title("Records")
r.geometry("1366x768+0+0")
# r.resizable(False, False)
con = sqlite3.connect("RegDatabase.db")
cur = con.cursor()
cur.execute("SELECT * FROM Register")
########Functions############
def ClearRecord():
NameLabelTxt.delete(0,END)
Contact_LabelTxt.delete(0, END)
Email_LabelTxt.delete(0, END)
Course_LabelTxt.delete(0, END)
Batch_LabelTxt.delete(0, END)
BatchTime_LabelTxt.delete(0, END)
Payment_LabelTxt.delete(0, END)
InvoiceNo_LabelTxt.delete(0, END)
def FindRecord():
con = sqlite3.connect("RegDatabase.db")
cursor = con.cursor()
global r
global NameLabelTxt
global Contact_LabelTxt
global Email_LabelTxt
global Course_LabelTxt
global Batch_LabelTxt
global BatchTime_LabelTxt
global Payment_LabelTxt
global InvoiceNo_LabelTxt
record_id = ID_UpdateDelete_.get()
cursor.execute("SELECT *FROM Register WHERE StudentID= " + record_id)
records = cursor.fetchall()
for record in records:
NameLabelTxt.insert(0, record[1])
Contact_LabelTxt.insert(0, record[2])
Email_LabelTxt.insert(0, record[3])
Course_LabelTxt.insert(0, record[4])
Batch_LabelTxt.insert(0, record[5])
BatchTime_LabelTxt.insert(0, record[6])
Payment_LabelTxt.insert(0, record[7])
InvoiceNo_LabelTxt.insert(0, record[8])
def updateRecord():
con = sqlite3.connect("RegDatabase.db")
cursor = con.cursor()
global r
global NameLabelTxt
global Contact_LabelTxt
global Email_LabelTxt
global Course_LabelTxt
global Batch_LabelTxt
global BatchTime_LabelTxt
global Payment_LabelTxt
global InvoiceNo_LabelTxt
record_id = ID_UpdateDelete_.get()
records = cursor.fetchall()
cursor.execute("""
UPDATE Register SET
Name = :name,
Contact = :con,
Email = :email,
Course = :cour,
Batch = :bat,
Batch_Time = :bat_time,
Payment = :pay,
InVoice_No = :inv
WHERE StudentID = :id""",
{
'name': NameLabelTxt.get(),
'email': Email_LabelTxt.get(),
'con': Contact_LabelTxt.get(),
'cour': Course_LabelTxt.get(),
'bat' : Batch_LabelTxt.get(),
'bat_time' : BatchTime_LabelTxt.get(),
'pay' : Payment_LabelTxt.get(),
'inv' : InvoiceNo_LabelTxt.get(),
'id': record_id
}
######GUI Part############
tree = ttk.Treeview(r)
tree['show'] = 'headings'
s = ttk.Style(r)
s.theme_use("winnative")
s.configure(".", font=('Microsoft YaHei UI Light', 11))
s.configure("Treeview.Heading", foreground='red', font=('Microsoft YaHei UI Light', 11, "bold"))
tree["columns"] = ("StudentID", "Name", "Contact", "Email", "Course", "Batch", "Batch_Time", "Payment", "Invoice_No")
tree.column("StudentID", width=50, minwidth=50, anchor=tk.CENTER)
tree.column("Name", width=150, minwidth=150, anchor=tk.CENTER)
tree.column("Contact", width=100, minwidth=100, anchor=tk.CENTER)
tree.column("Email", width=150, minwidth=150, anchor=tk.CENTER)
tree.column("Course", width=250, minwidth=250, anchor=tk.CENTER)
tree.column("Batch", width=220, minwidth=220, anchor=tk.CENTER)
tree.column("Batch_Time", width=150, minwidth=150, anchor=tk.CENTER)
tree.column("Payment", width=150, minwidth=150, anchor=tk.CENTER)
tree.column("Invoice_No", width=150, minwidth=150, anchor=tk.CENTER)
tree.heading("StudentID", text="Student ID", anchor=tk.CENTER)
tree.heading("Name", text="Name", anchor=tk.CENTER)
tree.heading("Contact", text="Contact", anchor=tk.CENTER)
tree.heading("Email", text="Email", anchor=tk.CENTER)
tree.heading("Course", text="Course", anchor=tk.CENTER)
tree.heading("Batch", text="Batch", anchor=tk.CENTER)
tree.heading("Batch_Time", text="Batch_Time", anchor=tk.CENTER)
tree.heading("Payment", text="Payment", anchor=tk.CENTER)
tree.heading("Invoice_No", text="Invoice_No", anchor=tk.CENTER)
i = 0
for ro in cur:
tree.insert('', i, text="", values=(ro[0], ro[1], ro[2], ro[3], ro[4], ro[5], ro[6], ro[7], ro[8]))
i = i + 1
NameLabel = Label(r, text="Name", font=('times new roman', 13, 'bold'), fg='gray20')
NameLabel.place(x=550, y=250)
NameLabelTxt = Entry(r, font=('times new roman', 13), bg='light gray', width='18')
NameLabelTxt.place(x=700, y=250)
Contact_Label = Label(r, text="Contact", font=('times new roman', 13, 'bold'), fg='gray20')
Contact_Label.place(x=545, y=290)
Contact_LabelTxt = Entry(r, font=('times new roman', 13), bg='light gray', width='18')
Contact_LabelTxt.place(x=700, y=290)
Email_Label = Label(r, text="Email", font=('times new roman', 13, 'bold'), fg='gray20')
Email_Label.place(x=549, y=330)
Email_LabelTxt = Entry(r, font=('times new roman', 13), bg='light gray', width='18')
Email_LabelTxt.place(x=700, y=330)
Course_Label = Label(r, text="Course", font=('times new roman', 13, 'bold'), fg='gray20')
Course_Label.place(x=545, y=372)
Course_LabelTxt = Entry(r, font=('times new roman', 13), bg='light gray', width='18')
Course_LabelTxt.place(x=700, y=373)
Batch_Label = Label(r, text="Batch", font=('times new roman', 13, 'bold'), fg='gray20')
Batch_Label.place(x=550, y=415)
Batch_LabelTxt = Entry(r, font=('times new roman', 13), bg='light gray', width='18')
Batch_LabelTxt.place(x=700, y=415)
BatchTime_Label = Label(r, text="Batch Time", font=('times new roman', 13, 'bold'), fg='gray20')
BatchTime_Label.place(x=530, y=460)
BatchTime_LabelTxt = Entry(r, font=('times new roman', 13), bg='light gray', width='18')
BatchTime_LabelTxt.place(x=700, y=459)
Payment_Label = Label(r, text="Payment", font=('times new roman', 13, 'bold'), fg='gray20')
Payment_Label.place(x=540, y=500)
Payment_LabelTxt = Entry(r, font=('times new roman', 13), bg='light gray', width='18')
Payment_LabelTxt.place(x=700, y=500)
InVoiceNo_Label = Label(r, text="In Voice No.", font=('times new roman', 13, 'bold'), fg='gray20')
InVoiceNo_Label.place(x=528, y=540)
InvoiceNo_LabelTxt = Entry(r, font=('times new roman', 13), bg='light gray', width='18')
InvoiceNo_LabelTxt.place(x=700, y=540)
ID_UpdateDelete_Label = Label(r, text="ID Update/Delete", font=('times new roman', 13, 'bold'), fg='gray20')
ID_UpdateDelete_Label.place(x=510, y=590)
ID_UpdateDelete_ = Entry(r, font=('times new roman', 13), bg='light gray', width='18')
ID_UpdateDelete_.place(x=700, y=590)
#######Scroll Bar widget########
hsb = ttk.Scrollbar(r, orient="horizontal")
rec = ttk.Scrollbar(r, orient="vertical")
hsb.configure(command=tree.xview)
rec.configure(command=tree.yview)
tree.configure(xscrollcommand=hsb.set)
# rec.configure(yscrollcommand=rec.set)
hsb.pack(fill=X, side=BOTTOM)
rec.pack(fill=Y, side=RIGHT)
FindRecord = Button(r, bd='5', width='10', text='Find', cursor='hand2', command=FindRecord)
FindRecord.place(x=500, y=650)
ClearRecord = Button(r, bd='5', width='10', text='Clear', cursor='hand2', command=ClearRecord)
ClearRecord.place(x=600, y=649)
btn_update = Button(r, bd='5', width='10', text="Update", cursor='hand2', command=updateRecord)
btn_update.place(x=700, y=649)
tree.pack()
r.mainloop()
)
con.commit()
con.close()
print("Record Updated Successfully!!!!")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
