I have a backend file and a frontend file, and I want it so that for the view function, it queries the database and outputs each row (on a new line) to the Listbox widget.
How can I edit my code so that it 1) Outputs to the Listbox widget rather than printing into the console and 2) Outputs each database row on a new row in the Listbox widget?
I can't seem to figure it out and I can't find any resources.
Thank you
Here's my backend code:
def connect():
conn=sqlite3.connect('Orders.db')
cur=conn.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS stuffToPlot(Reference REAL, Date TEXT, Name TEXT, SubTotal REAL, VAT REAL, Total REAL)")
conn.commit()
conn.close()
def view():
conn = sqlite3.connect('Orders.db')
cur = conn.cursor()
cur.execute("SELECT * FROM stuffToPlot")
rows=cur.fetchall()
conn.close()
print (rows)
And here's my frontend code:
from tkinter import *
from backend import hello
from backend import connect
from backend import insert
from backend import view
from backend import search
window = Tk()
l1=Label(window, text ="Order Reference ID")
l1.grid(row=0,column=0)
l1=Label(window, text ="Date")
l1.grid(row=0,column=2)
l1=Label(window, text ="Name")
l1.grid(row=1,column=0)
l1=Label(window, text ="SubTotal")
l1.grid(row=1,column=2)
l1=Label(window, text ="VAT")
l1.grid(row=2,column=0)
l1=Label(window, text ="Total Cost")
l1.grid(row=2,column=2)
referenceID_text=StringVar()
e1=Entry(window,textvariable=referenceID_text)
e1.grid(row=0,column=1)
date_text=StringVar()
e2=Entry(window,textvariable=date_text)
e2.grid(row=0,column=3)
name_text=StringVar()
e3=Entry(window,textvariable=name_text)
e3.grid(row=1,column=1)
subtotal_text=StringVar()
e4=Entry(window,textvariable=subtotal_text)
e4.grid(row=1,column=3)
vat_text=StringVar()
e3=Entry(window,textvariable=vat_text)
e3.grid(row=2,column=1)
total_text=StringVar()
e4=Entry(window,textvariable=total_text)
e4.grid(row=2,column=3)
list1=Listbox(window, height=6,width=35)
list1.grid(row=2,column=0,rowspan=6,columnspan=2)
#Attatch scrollbar
sb1=Scrollbar(window)
sb1.grid(row=2,column=2,rowspan=6)
list1.configure(yscrollcommand=sb1.set)
sb1.configure(command=list1.yview)
#Define buttons
b1=Button(window,text="View All", width=15, command=lambda: view())
b1.grid(row=3,column=3)
b1=Button(window,text="Search Entry", width=15, command=lambda: search())
b1.grid(row=4,column=3)
b1=Button(window,text="Close", width=15)
b1.grid(row=5,column=3)
b1=Button(window,text="Add Entry", width=15, command=lambda: insert())
b1.grid(row=6,column=3)
b1=Button(window,text="Delete Selected", width=15, command=lambda: delete())
b1.grid(row=7,column=3)
connect()
window.mainloop()
The simple way is to insert
directly the rows returned by the query in the Listbox
widget.
First change the function view
to return the rows
:
def view():
conn = sqlite3.connect('Orders.db')
cur = conn.cursor()
cur.execute("SELECT * FROM stuffToPlot")
rows=cur.fetchall()
conn.close()
return rows
Then create another function that loop on the results to insert them :
def insert_rows():
for row in view():
list1.insert(END, row)
b1=Button(window,text="View All", width=15, command=lambda: insert_rows())
Now if you want a better output value, you could format a string with a syntax like :
list1.insert(END, '{} {} {} {} {} Total : {}'.format(*row))
(Just make sure about the order of fields by naming them in your query)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.