从TTK组合框列表中删除项目

问题描述 投票:0回答:1

我目前正在尝试找出如何从组合框下拉列表中删除项目。目前,对于我的删除按钮,我具有以下代码:

def prof_del_btn():
    conn = sqlite3.connect('Doom.db')
    c = conn.cursor()

    name=prof_input.get()
    c.execute("SELECT prof FROM profile")
    rows=c.fetchall()

    if (name,) in rows:
        delProf = c.execute('DELETE from profile WHERE prof = "{}"'.format(name))
        prof_input.set('')
        print('Profile {} has been deleted'.format(name))
        prof_input['values'] = (prof_input['values'])
    else:
        prof_input.set('')
        print('Profile Doesn\'t Exist')

    conn.commit()
    conn.close()

[请注意,我在玩它,只是无法弄清楚。当前在我的sql数据库中,我在配置文件下有“ nat”和“ two”,而我想做的是删除“ two”。

我目前不使用类,因为目前我不太确定如何正确使用它们。我将如何重新加载列表?

python tkinter combobox ttk
1个回答
0
投票

您可以从values中获取当前的Combobox,然后从列表中删除所需的项目,然后更新values。同样,您不需要在SELECT语句之前执行DELETE语句。下面是修改后的代码:

def prof_del_btn():
    name = prof_input.get()
    if name:
        with sqlite3.connect('doom.db') as conn:
            c = conn.cursor()
            c.execute('DELETE FROM profile WHERE prof = ?', (name,))
            if c.rowcount > 0:
                print('Profile {} has been deleted'.format(name))
                options = list(prof_input['values'])
                options.remove(name)
                prof_input['values'] = options
            else:
                print('Profile {} does not exist'.format(name))
            prof_input.set('')
            conn.commit()
© www.soinside.com 2019 - 2024. All rights reserved.