![](/img/trans.png)
[英]So basically I need to take data a user inputs into a Tkinter entry box and store it into my sqlite3 database. I am using Python 3.6
[英]what tkinter widget should use to display and store data? Its okay to use a label? I'm using sqlite3 for database
def query(): global record conn = sqlite3.connect('billing.db') c = conn.cursor() c.execute("SELECT *, oid FROM billing") records = c.fetchall() # print(records) print_records = '' for record in records: print_records += str(record[0]) + " " + str(record[1]) + " " + str(record[2]) \ + " " + str(record[3]) + " " + str(record[4]) + " " + str(record[5]) \ + " " + str(record[6]) + " " + str(record[7]) + "\n" show = Label(root, width=120, text=print_records, anchor=NW) show.grid(row=2, column=3, columnspan=2) show.place(x=402, y=50, height=510) show.config(font=("TimesNewRoman", 10)) conn.commit() conn.close()
这是一个非常简单的示例,使用ttk.Treeview
显示数据库中的数据:
def query():
# Same code above...
records = [('Hello','World'),('This','is'),('Treeview','widget')] # This will c.fetchall()
cols = ('First column','Second columns') # Change this with whatever you want your column name to be
tree = ttk.Treeview(root,columns=cols,show='headings',height=100)
tree.pack(padx=10,pady=10)
for col in cols: # Set treeview attributes
tree.heading(col,text=col)
tree.column(col,width=100,anchor='center')
for items in records: # Insert values inside the treeview
tree.insert('','end',values=items)
请记住,由于这是一个ttk
小部件,因此您必须先导入它:
from tkinter import ttk
如果您希望列具有单独的宽度,请检查此答案
一旦我单击查询以显示在 treeview 小部件中,它就会复制数据库中的所有记录。 如何删除重复记录并仅显示已添加到数据库中的记录?
def show(self):
global record
conn = sqlite3.connect('billing.db')
c = conn.cursor()
c.execute("SELECT *, oid FROM billing")
self.records = c.fetchall()
for items in self.records:
self.table.insert('', 'end', values=items)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.