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

Tkinter查询数据旧内容未清除问题求助

问题解决:Tkinter切换Combobox选项后旧数据残留

问题描述

使用Tkinter创建窗口,通过Combobox从SQLite3数据库选择用户姓名,展示对应行的邮箱、地址等数据。但切换选择其他姓名时,之前的数据仍会显示,尝试在ZoekNAW函数开头设置Label的text为空无效。

问题原因

  1. 重复创建Label控件:每次调用ZoekNAW时,都会新建一组Label并放置在相同位置,旧的Label并未被销毁,若新文本长度更短,旧文本的部分内容会残留显示。
  2. SQL语句存在注入风险:直接用字符串拼接构建SQL查询,存在SQL注入安全隐患,同时可能因姓名含特殊字符导致查询失败。

修复后的代码

import tkinter as tk
from tkinter import ttk
import sqlite3

# 设置Combobox数据:从数据库获取姓名
def windowNAW():
    gebruikers = []
    sqliteConnection = sqlite3.connect('ZHSys-NAW.db')
    cursor = sqliteConnection.cursor()
    cursor.execute("SELECT achternaam FROM gebruikers")
    for row in cursor.fetchall():
        gebruikers.append(row[0])
    cursor.close()
    sqliteConnection.close()

    windowNAW = tk.Tk()
    windowNAW.title("Gegevens bij gebruikers zoeken")   
    windowNAW.geometry("500x500")
    
    IntroNAW = tk.Label(windowNAW, text="Zoek gegevens bij bestaande gebruikers. Selecteer een gebruiker.", font='Verdana 10')
    IntroNAW.place(x=10, y=60)

    CBZoekGebruiker = ttk.Combobox(windowNAW, values=gebruikers, state="readonly")
    CBZoekGebruiker.place(x=10, y=100)

    # 创建用于展示数据的Label,保存引用以便后续更新
    email_label = tk.Label(windowNAW, text="", font="Verdana 10 bold")
    email_label.place(x=200, y=233)
    
    adres_label = tk.Label(windowNAW, text="", font="Verdana 10 bold")
    adres_label.place(x=200, y=263)
    
    woonplaats_label = tk.Label(windowNAW, text="", font="Verdana 10 bold")
    woonplaats_label.place(x=200, y=293)

    # 查询并更新用户数据
    def ZoekNAW():
        # 先清空现有文本
        email_label.config(text="")
        adres_label.config(text="")
        woonplaats_label.config(text="")
        
        ZoekGebruiker = CBZoekGebruiker.get()
        if not ZoekGebruiker:
            return
        
        sqliteConnection = sqlite3.connect('ZHSys-NAW.db')
        cursor = sqliteConnection.cursor()
        # 使用参数化查询避免SQL注入
        cursor.execute("SELECT * FROM gebruikers WHERE Achternaam=?", (ZoekGebruiker,))
        rows = cursor.fetchall()
        for data in rows:
            # 直接更新已有Label的文本
            email_label.config(text=f"{data[1]}")
            adres_label.config(text=f"{data[4]}")
            woonplaats_label.config(text=f"{data[5]}")
        cursor.close()
        sqliteConnection.close()

    tk.Button(windowNAW, text="Zoek", command=ZoekNAW).place(x=10, y=150)

    tk.Label(windowNAW, text="Email: ", font="Verdana 10 bold").place(x=10, y=230)
    tk.Label(windowNAW, text="Adres: ", font="Verdana 10 bold").place(x=10, y=260)
    tk.Label(windowNAW, text="Woonplaats: ", font="Verdana 10 bold").place(x=10, y=290)

    windowNAW.mainloop()

if __name__ == "__main__":
    windowNAW()

关键修复点

  • 复用Label控件:在窗口初始化时创建用于展示数据的Label并保存引用,后续仅通过config(text=...)更新文本内容,避免重复创建控件导致的旧数据残留。
  • 前置清空逻辑:查询前先清空三个Label的文本,确保切换用户时旧数据被彻底清除。
  • 参数化SQL查询:替换字符串拼接为参数化查询,消除SQL注入风险,同时兼容含特殊字符的用户姓名。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 05:53:08