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

Tkinter密码管理器:指定行编辑与数据库更新功能故障排查

Tkinter密码管理器编辑功能修复

核心问题及修复方案

问题1:edit_password仅能编辑最后一行内容

原因:循环生成条目时,edit_password函数未绑定当前行的Text组件,循环结束后函数始终指向最后一行的text变量。

修复:用partial将当前行的四个Text组件作为参数传入edit_password,让函数仅启用当前行的编辑状态:

# 重新定义edit_password,接收当前行的Text组件
def edit_password(txt1, txt2, txt3, txt4):
    txt1.config(state="normal")
    txt2.config(state="normal")
    txt3.config(state="normal")
    txt4.config(state="normal")

# 循环中创建编辑按钮时绑定当前行组件
edit_button = tk.Button(my_frame, text="Update information", 
                        command=partial(edit_password, text1, text2, text3, text4))
edit_button.grid(column=5, row=i+4, pady=8, padx=10)

问题2:save_new_password触发SQL语法错误

原因:UPDATE语句使用了错误格式(误用VALUES),且缺少WHERE条件会更新所有行;同时Text组件获取的内容自带换行符,需处理。

修复:修正SQL UPDATE语法,添加WHERE id=?指定更新条目,并用strip()去除换行:

# 重新定义save_new_password,接收条目ID和当前行组件
def save_new_password(entry_id, txt1, txt2, txt3, txt4):
    # 去除文本末尾换行符
    website = txt1.get("1.0", "end").strip()
    username = txt2.get("1.0", "end").strip()
    password = txt3.get("1.0", "end").strip()
    day = txt4.get("1.0", "end").strip()
    
    # 正确的UPDATE语句格式
    update_query = """UPDATE vault 
                      SET website=?, username=?, password=?, day=? 
                      WHERE id=?"""
    cursor.execute(update_query, (website, username, password, day, entry_id))
    db.commit()
    
    # 恢复组件禁用状态
    txt1.config(state="disabled")
    txt2.config(state="disabled")
    txt3.config(state="disabled")
    txt4.config(state="disabled")
    password_vault()

# 循环中创建保存按钮时绑定条目ID和组件
password_save_button = tk.Button(my_frame, text="Save your updated information",
                                 command=partial(save_new_password, array[i][0], text1, text2, text3, text4))
password_save_button.grid(column=6, row=i+4, pady=8, padx=10)

完整修改后的password_vault函数关键片段

def password_vault():
    for widget in window.winfo_children():
        widget.destroy()

    # 保留原有add_entry、remove_entry函数...

    # 修改后的edit_password函数
    def edit_password(txt1, txt2, txt3, txt4):
        txt1.config(state="normal")
        txt2.config(state="normal")
        txt3.config(state="normal")
        txt4.config(state="normal")

    # 保留界面布局代码...

    cursor.execute("SELECT * FROM vault")
    entries = cursor.fetchall()
    if entries:
        for i, entry in enumerate(entries):
            entry_id = entry[0]
            # 创建当前行的Text组件
            text1 = tk.Text(my_frame, height=1, width=20, state="disabled")
            text1.insert("1.0", entry[1])
            text1.grid(column=0, row=i+4)
            
            text2 = tk.Text(my_frame, height=1, width=20, state="disabled")
            text2.insert("1.0", entry[2])
            text2.grid(column=1, row=i+4)
            
            text3 = tk.Text(my_frame, height=1, width=20, state="disabled")
            text3.insert("1.0", entry[3])
            text3.grid(column=2, row=i+4)
            
            text4 = tk.Text(my_frame, height=1, width=20, state="disabled")
            text4.insert("1.0", entry[4])
            text4.grid(column=3, row=i+4)

            # 删除按钮(保留原有逻辑)
            delete_button = tk.Button(my_frame, text="Delete", command=partial(remove_entry, entry_id))
            delete_button.grid(column=4, row=i+4, pady=8, padx=10)

            # 绑定当前行的编辑按钮
            edit_button = tk.Button(my_frame, text="Update information", 
                                    command=partial(edit_password, text1, text2, text3, text4))
            edit_button.grid(column=5, row=i+4, pady=8, padx=10)

            # 修改后的保存函数
            def save_new_password(entry_id, txt1, txt2, txt3, txt4):
                website = txt1.get("1.0", "end").strip()
                username = txt2.get("1.0", "end").strip()
                password = txt3.get("1.0", "end").strip()
                day = txt4.get("1.0", "end").strip()
                
                update_query = """UPDATE vault 
                                  SET website=?, username=?, password=?, day=? 
                                  WHERE id=?"""
                cursor.execute(update_query, (website, username, password, day, entry_id))
                db.commit()
                
                txt1.config(state="disabled")
                txt2.config(state="disabled")
                txt3.config(state="disabled")
                txt4.config(state="disabled")
                password_vault()

            # 绑定当前条目ID的保存按钮
            password_save_button = tk.Button(my_frame, text="Save your updated information",
                                             command=partial(save_new_password, entry_id, text1, text2, text3, text4))
            password_save_button.grid(column=6, row=i+4, pady=8, padx=10)

内容的提问来源于stack exchange,提问作者initial-commiter

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 16:02:02