I tried to use tree view to display the data of my project output but consistently got below error for all the button clicks
I then switched to Listbox view and I was able to execute my project with no errors, all data being displayed, scrollbar working and select working perfectly.
How I want to use the tree view and also get it to work with selection and scrollbar. If anyone can assist correct my code to use the tree view instead and point out my mistakes. I have commented the tree view code.
from tkinter import *
from app_backend import Database
from tkinter import ttk
class Database:
def __init__(self):
self.conn = pymysql.connect(host="localhost",user="usrname",
password="",database="database",connect_timeout=10)
self.cur = self.conn.cursor()
self.cur.execute("SELECT * FROM property_damage_claim")
self.conn.ping(reconnect=True)
#we will leave the database open here.
def insert(self,clf_name,cll_name,contact,policy,Insurance,in_contact,status):
self.conn.ping(reconnect=True)
self.cur.execute("INSERT INTO property_damage_claim VALUES (
NULL,%s,%s,%s,%s,%s,%s,%s)",(clf_name,cll_name,contact,policy,
Insurance,in_contact,status))
self.conn.commit()
def view(self):
self.conn.ping(reconnect=True)
self.cur.execute("SELECT * FROM property_damage_claim")
rows = self.cur.fetchall()
return rows
def search(self,clf_name ='',cll_name ='',contact ='',policy='',Insurance='',in_contact='',status=''):
self.conn.ping(reconnect=True)
self.cur.execute("SELECT * FROM property_damage_claim WHERE clf_name=%s OR
cll_name=%s OR contact=%s OR policy=%s OR Insurance=%s OR in_contact=%s OR
status=%s",(clf_name,cll_name,contact,policy,Insurance,in_contact,status))
rows = self.cur.fetchall()
return rows
def delete(self,id):
self.conn.ping(reconnect=True)
self.cur.execute("DELETE FROM property_damage_claim WHERE id=%s",(id,))
self.conn.commit()
def update(self,id,clf_name,cll_name,contact,policy,Insurance,in_contact,status):
self.conn.ping(reconnect=True)
self.cur.execute("UPDATE property_damage_claim SET clf_name=%s, cll_name=%s, contact=%s,
policy=%s, Insurance=%s, in_contact=%s, status=%s WHERE id=%s",(clf_name,cll_name,
contact,policy,Insurance,in_contact,status,id))
self.conn.commit()
def __del__(self):
self.conn.close()
db = Database()
def selected_row(event):
try:
global selected_content
index = student_list.curselection()[0]
selected_content = student_list.get(index)
e1.delete(0,END)
e1.insert(END,selected_content[1])
e2.delete(0,END)
e2.insert(END,selected_content[2])
e3.delete(0,END)
e3.insert(END,selected_content[3])
e4.delete(0,END)
e4.insert(END,selected_content[4])
e5.delete(0,END)
e5.insert(END,selected_content[5])
e6.delete(0,END)
e6.insert(END,selected_content[6])
e7.delete(0,END)
e7.insert(END,selected_content[7])
except IndexError:
pass
# connecting the backend functions
def view_all():
student_list.delete(0,END)
for row in db.view():
student_list.insert(END,row)
def search_record():
student_list.delete(0,END)
for row in db.search(clf_name.get(),cll_name.get(),contact.get(),
policy.get(),Insurance.get(),in_contact.get(),status.get()):
student_list.insert(END,row)
def add_record():
try:
if cll_name.get():
db.insert(clf_name.get(),cll_name.get(),contact.get(),policy.get(),
Insurance.get(),in_contact.get(),status.get())
student_list.delete(0,END)
student_list.insert(END,(clf_name.get(),cll_name.get(),contact.get(),
policy.get(),Insurance.get(),in_contact.get(),status.get()))
e1.delete(0,END)
e2.delete(0,END)
e3.delete(0,END)
e4.delete(0,END)
e5.delete(0,END)
e6.delete(0,END)
e7.delete(0,END)
except:
print("tcl Error")
def delete_record():
db.delete(selected_content[0])
e1.delete(0,END)
e2.delete(0,END)
e3.delete(0,END)
e4.delete(0,END)
e5.delete(0,END)
e6.delete(0,END)
e7.delete(0,END)
view_all()
def update_record():
db.update(selected_content[0],clf_name.get(),cll_name.get(),
contact.get(),policy.get(),Insurance.get(),in_contact.get(),status.get())
view_all()
def new_record():
e1.delete(0,END)
e2.delete(0,END)
e3.delete(0,END)
e4.delete(0,END)
e5.delete(0,END)
e6.delete(0,END)
e7.delete(0,END)
window = Tk()
window.geometry("1000x2000")
bg = PhotoImage(file = "bgg.gif")
label1 = Label( window, image = bg)
label1.place(x = 0, y = 0)
window.wm_title("Goodwill Brokers")
wr1=LabelFrame(window, text="")
wr2=LabelFrame(window, text="Client Data")
wr3=LabelFrame(window, text="Search")
wr4=LabelFrame(window, text="Client List")
wr1.pack(fill="both", expand="no", padx="20", pady="10")
wr2.pack(fill="both", expand="no", padx="20", pady="10")
wr3.pack(fill="both", expand="no", padx="20", pady="10")
wr4.pack(fill="both", expand="no", padx="20", pady="10")
# GUI Components
# Labels
lb_title = ttk.Label(wr1,text="Goodwill Brokers".upper())
lb_title.grid(row=0,column=0,columnspan=4,pady=15)
lb_date = ttk.Label(wr2,text="First Name:")
lb_date.grid(row=4,column=0,sticky="e")
lb_sname = ttk.Label(wr2,text="Last Name:")
lb_sname.grid(row=5,column=0,sticky="e")
lb_grade = ttk.Label(wr2,text="Contact:")
lb_grade.grid(row=6,column=0,sticky="e")
lb_class = ttk.Label(wr2,text="Policy:")
lb_class.grid(row=7,column=0,sticky="e")
lb_pname = ttk.Label(wr2,text="Insurance:")
lb_pname.grid(row=4,column=2,sticky="e")
lb_psign = ttk.Label(wr2,text="Contact Person/Entity:")
lb_psign.grid(row=5,column=2,sticky="e")
lb_cin = ttk.Label(wr2,text="Status:")
lb_cin.grid(row=6,column=2,sticky="e")
# Entries
clf_name = StringVar()
e1 = ttk.Entry(wr2,textvariable= clf_name)
e1.grid(row =4,column=1)
cll_name = StringVar()
e2 = ttk.Entry(wr2,textvariable= cll_name)
e2.grid(row =5,column=1)
contact = StringVar()
e3 = ttk.Entry(wr2,textvariable= contact)
e3.grid(row =6,column=1)
policy = StringVar()
e4 = ttk.Entry(wr2,textvariable= policy)
e4.grid(row =7,column=1)
Insurance = StringVar()
e5 = ttk.Entry(wr2,textvariable= Insurance)
e5.grid(row =4,column=3)
in_contact = StringVar()
e6 = ttk.Entry(wr2,textvariable= in_contact)
e6.grid(row =5,column=3)
status = StringVar()
e7 = ttk.Entry(wr2,textvariable= status)
e7.grid(row =6,column=3)
# Buttons
b1 = ttk.Button(wr3,text="View All", width=23,command=view_all)
b1.grid(row=8,column=0,columnspan=2,sticky="e",padx=10,pady=6)
b2 = ttk.Button(wr3,text="Search", width=23,command=search_record)
b2.grid(row=8,column=2,columnspan=2,sticky="w",padx=10,pady=6)
b3 = ttk.Button(wr3,text="Add", width=23,command=add_record)
b3.grid(row=9,column=0,columnspan=2,sticky="e",padx=10,pady=6)
b4 = ttk.Button(wr3,text="Update", width=23,command=update_record)
b4.grid(row=9,column=2,columnspan=2,sticky="w",padx=10,pady=6)
b5 = ttk.Button(wr3,text="Delete", width=23,command=delete_record)
b5.grid(row=10,column=0,columnspan=2,sticky="e",padx=10,pady=6)
b6 = ttk.Button(wr3,text="Close", width=23,command=window.destroy)
b6.grid(row=11,column=0,columnspan=2,sticky=E,padx=10,pady=6)
b7 = ttk.Button(wr3,text="Clear", width=23,command=new_record)
b7.grid(row=10,column=2,columnspan=2,sticky="w",padx=10,pady=6)
# Listbox and scrollbar
student_list= Listbox(wr4,height=6,width=60)
student_list.grid(row=12,column=0,rowspan=6,columnspan=3,sticky=E,padx=10,pady=10)
''' student_list = ttk.Treeview(wr4, columns=(1,2,3,4,5,6,7), show='headings')
student_list.grid()
student_list.column("#1", anchor=CENTER)
student_list.heading("#1", text="ID")
student_list.column("#2", anchor=CENTER)
student_list.heading("#2", text="FNAME")
student_list.column("#3", anchor=CENTER)
student_list.heading("#3", text="LNAME")
student_list.column("#4", anchor=CENTER)
student_list.heading("#4", text="LNAME")
student_list.column("#5", anchor=CENTER)
student_list.heading("#5", text="LNAME")
student_list.column("#6", anchor=CENTER)
student_list.heading("#6", text="LNAME")
student_list.column("#7", anchor=CENTER)
student_list.heading("#7", text="LNAME")
'''
sb1 = Scrollbar(wr4)
sb1.grid(row=12,column=3,rowspan=6,sticky=W)
student_list.configure(yscrollcommand=sb1.set)
sb1.configure(command=student_list.yview)
# get the selected row from Listox to use Delete and Update Commands
student_list.bind('<<ListboxSelect>>',selected_row)
window.resizable(0,0)
window.mainloop()