[英]My Tkinter code for displaying every field from a database isn't displaying in the tkinter widget
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. 我有一个后端文件和一个前端文件,我想要它,以便对视图函数来说,它查询数据库并将每行(在新行中)输出到Listbox小部件。
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? 如何编辑我的代码,使其成为1)输出到“列表框”小部件而不是打印到控制台中,以及2)将“数据库”行中的每个数据库行输出到新行?
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. 一种简单的方法是
insert
查询返回的行直接insert
到Listbox
小部件中。
First change the function view
to return the rows
: 首先更改函数
view
以返回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) (只需在查询中命名字段即可确定其顺序)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.