You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

tkinter+sqlite3产品管理应用新增条目时如何刷新现有标签列表

需求说明

我正在使用tkinter和sqlite3开发一款产品管理应用。我希望通过标签组件展示产品数据,目前该功能运行正常,但我还想实现新增条目时先删除所有已有的产品标签,再重新生成新标签的逻辑,以此实现新增条目后产品列表自动更新的效果,否则用户需要重启应用才能看到新增的条目。

原有问题

每次新增产品后调用showProducts只会追加新的标签,原有标签不会消失,导致产品列表重复展示旧数据;同时原有SQL拼接写法存在注入风险,删除按钮也未调用pack方法无法显示。

解决方案

只需要修改showProducts函数,在生成新的产品标签前,清空容器内除了固定标题之外的所有历史组件即可,同时优化存在问题的细节逻辑。

修改点说明

  1. 在showProducts函数开头添加清空历史组件的逻辑,避免旧标签残留
  2. 替换原有的SQL字符串拼接为参数化查询,提升安全性
  3. 补充删除按钮的pack调用,让按钮正常显示

修改后的完整代码

from tkinter import *
import sqlite3

# 表名 = items
conn = sqlite3.connect("productManagement.db")
cursor = conn.cursor()

itemsSearch = cursor.execute("SELECT rowid, * FROM items")
itemsFetch = itemsSearch.fetchall()

window = Tk()
window.geometry("800x600")
window.config(bg="#9BB7D4")

# 功能定义
itemFrame = Frame(bg="#8a8a8a",width=200,height=200)
frameTitle = Label(itemFrame,text="产品列表:",bg="#8a8a8a",font=("Arial Black",12))
frameTitle.pack()

def createProduct():
    global itemsFetch
    name = nameEntry.get()
    price = priceEntry.get()
    quantity = quantityEntry.get()
    number = numberEntry.get()

    # 参数化查询避免SQL注入风险
    cursor.execute("INSERT INTO items VALUES (?,?,?,?)", (name, int(price), int(quantity), int(number)))
    conn.commit()

    itemsSearch = cursor.execute("SELECT rowid, * FROM items")
    itemsFetch = itemsSearch.fetchall()
    showProducts()

def showProducts():
    global itemStats
    # 清空itemFrame中除了标题之外的所有历史组件
    for widget in itemFrame.winfo_children()[1:]:
        widget.destroy()

    for item in itemsFetch:
        itemStats = Label(itemFrame,bg="#8a8a8a",font=("Italic",12),
        text=f"名称: {item[1]}     价格: {int(item[2])}€     库存: {int(item[3])}     产品编号: {int(item[4])}")
        deleteBtn = Button(itemFrame,text="删除")
        itemStats.pack()
        deleteBtn.pack() # 补充按钮渲染逻辑

showProducts()

# GUI组件定义
title = Label(text="产品管理系统",font=("Arial Black",24),bg="#9BB7D4")
nameText = Label(text="名称:",bg="#9BB7D4",font=("Italic",12))
nameEntry = Entry(font=("Italic",12))
priceText = Label(text="价格:",bg="#9BB7D4",font=("Italic",12))
priceEntry = Entry(font=("Italic",12))
quantityText = Label(text="库存:",bg="#9BB7D4",font=("Italic",12))
quantityEntry = Entry(font=("Italic",12))
numberText = Label(text="产品编号:",bg="#9BB7D4",font=("Italic",12))
numberEntry = Entry(font=("Italic",12))
createProductBtn = Button(text="创建产品",command=createProduct)

# 布局设置
title.pack(pady=20)
nameText.place(x=140,y=140)
nameEntry.place(x=190,y=140)
priceText.place(x=340,y=140)
priceEntry.place(x=387,y=140)
quantityText.place(x=125,y=200)
quantityEntry.place(x=190,y=200)
numberText.place(x=340,y=200)
numberEntry.place(x=463,y=200)
createProductBtn.place(x=190,y=260)
itemFrame.place(x=190,y=300)

conn.commit()
window.mainloop()
conn.close()

内容的提问来源于stack exchange,提问作者Fovu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 20:36:03