0

我正在为我和我的朋友小商店创建一个程序。我正在使用 Python、Sqlite3 和 Tkinter。我能够通过 Tkinter 显示来自 Sqlite3 的记录,但是当我插入新记录/数据时,它会插入到 Sqlite3 中,但 Tkinter 不显示插入的新记录。这是我的代码;我希望这是可以理解的。

import tkinter
from tkinter import*
from tkinter import ttk, LabelFrame
import tkinter.messagebox
import sqlite3


conn = sqlite3.connect('new1.db')


def update(show):
    for i in show:
        trv.insert('', 'end', values=i)


def submitprod():

   conn = sqlite3.connect('new1.db')

   c = conn.cursor()
   c.execute("INSERT INTO products VALUES (:pdesc, :qty, :prce, :uprce)",
             {
                  'pdesc': pdesc.get(),
                  'qty': qty.get(),
                  'prce': prce.get(),
                  'uprce': uprce.get()


             })

   conn.commit()

#reset
   pdesc.delete(0, END)
   qty.delete(0, END)
   prce.delete(0, END)
   uprce.delete(0, END)


c = conn.cursor ()
top = tkinter.Tk()

box1 = LabelFrame(top, text="Product Entry")
box1.pack (fill="both", expand="yes", padx=20, pady=10)
box2 = LabelFrame(top, text="Products")
box2.pack (fill="both", expand="yes", padx=20, pady=10)

#product labels and entry

pdesc = Entry(box1, width=30)
pdesc.grid(row=1, column=4, padx=20)
qty = Entry(box1, width=30)
qty.grid(row=2, column=4, padx=20)
prce = Entry(box1, width=30)
prce.grid(row=3, column=4, padx=20)
uprce = Entry(box1, width=30)
uprce.grid(row=4, column=4, padx=20)



pdesc_label = Label(box1, text='Product')
pdesc_label.grid(row=1, column=5)
qty_label = Label(box1, text='Quantity')
qty_label.grid(row=2, column=5)
prce_label = Label(box1, text='Price')
prce_label.grid(row=3, column=5)
uprce_label = Label(box1, text='Unit Price')
uprce_label.grid(row=4, column=5)

#products
trv = ttk.Treeview(box2, column=(1,2,3,4,5,6), show="headings", height="20")
style=ttk.Style(trv)
style.configure('Treeview', rowheight=20)

trv.pack(side=LEFT)
trv.heading(1, text="Product ID")
trv.heading(2, text="Product Description")
trv.heading(3, text="Quantity")
trv.heading(4, text="Price")
trv.heading(5, text="Unit Price")
trv.heading(6, text="Return Percentage")

#data for products
conn = sqlite3.connect('new1.db')

c = conn.cursor()
query = "SELECT oid, productdesc, qty, price, uprice from products"
c.execute(query)
show = c.fetchall()
update(show)

btn2 = ttk.Button(box1, text='Enter', command=submitprod)
btn2.grid(row=6, column=4, columnspan=1, pady=10, padx=10, ipadx=10)

top.title("Test")
top.geometry("1500x1200")
top.mainloop()
4

1 回答 1

0

正如布莱恩奥克利指出的那样。您目前仅在初始程序运行时查询您的 SQL 数据库。此外,当您运行submitprod()函数时,您需要调用update()函数将新数据添加到树视图中。

我建议对您当前的代码进行以下调整。

添加查询数据库功能,可以查询最新的产品数据。这可以在您的submitprod()函数和初始程序运行中调用。

添加数据库插入功能。您可以将这两个都添加到您的submitprod()函数中,然后从那里更新您的树视图。

import tkinter
from tkinter import*
from tkinter import ttk, LabelFrame
import tkinter.messagebox
import sqlite3


def update(show):
    for i in show:
        trv.insert('', 'end', values=i)

def query_database():
    query = "SELECT oid, productdesc, qty, price, uprice from products"

    conn = sqlite3.connect('new1.db')
    c = conn.cursor()
    c.execute(query)
    show = c.fetchall()

    return show

def database_insert():
    conn = sqlite3.connect('new1.db')
    c = conn.cursor()
    c.execute("INSERT INTO products VALUES (:pdesc, :qty, :prce, :uprce)",{
          'pdesc': pdesc.get(),
          'qty': qty.get(),
          'prce': prce.get(),
          'uprce': uprce.get()})

    conn.commit()

def submitprod():
    database_insert()
    current_db_data = query_database()
    update(current_db_data)
    
    #reset
    pdesc.delete(0, END)
    qty.delete(0, END)
    prce.delete(0, END)
    uprce.delete(0, END)

top = tkinter.Tk()
top.title("Test")
top.geometry("1500x1200")

box1 = LabelFrame(top, text="Product Entry")
box1.pack (fill="both", expand="yes", padx=20, pady=10)
box2 = LabelFrame(top, text="Products")
box2.pack (fill="both", expand="yes", padx=20, pady=10)

#product labels and entry

pdesc = Entry(box1, width=30)
pdesc.grid(row=1, column=4, padx=20)
qty = Entry(box1, width=30)
qty.grid(row=2, column=4, padx=20)
prce = Entry(box1, width=30)
prce.grid(row=3, column=4, padx=20)
uprce = Entry(box1, width=30)
uprce.grid(row=4, column=4, padx=20)
    
pdesc_label = Label(box1, text='Product')
pdesc_label.grid(row=1, column=5)
qty_label = Label(box1, text='Quantity')
qty_label.grid(row=2, column=5)
prce_label = Label(box1, text='Price')
prce_label.grid(row=3, column=5)
uprce_label = Label(box1, text='Unit Price')
uprce_label.grid(row=4, column=5)

#products
trv = ttk.Treeview(box2, column=(1,2,3,4,5,6), show="headings", height="20")
style=ttk.Style(trv)
style.configure('Treeview', rowheight=20)

trv.pack(side=LEFT)
trv.heading(1, text="Product ID")
trv.heading(2, text="Product Description")
trv.heading(3, text="Quantity")
trv.heading(4, text="Price")
trv.heading(5, text="Unit Price")
trv.heading(6, text="Return Percentage")

btn2 = ttk.Button(box1, text='Enter', command=submitprod)
btn2.grid(row=6, column=4, columnspan=1, pady=10, padx=10, ipadx=10)

# load database data on initial startup of app
initial_data = query_database()
update(initial_data)

top.mainloop()

使用 SQL 数据库时要记住的一件事是需要关闭游标和连接。SQLite 特别不喜欢并发查询数据库,如果您的应用程序数据库规模增长,请注意这一点。

于 2020-10-09T21:41:12.290 回答