Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不确定如何在连接数据库的情况下更新python中的combobox_Python_Sqlite_Tkinter_Combobox - Fatal编程技术网

不确定如何在连接数据库的情况下更新python中的combobox

不确定如何在连接数据库的情况下更新python中的combobox,python,sqlite,tkinter,combobox,Python,Sqlite,Tkinter,Combobox,我想知道如何更新tkinter中组合框下的下拉列表。 这就是我目前拥有的: from tkinter import * from tkinter import ttk import sqlite3 #profile system def prof_value_input(): conn = sqlite3.connect('data.db') c = conn.cursor() query = c.execute('SELECT prof FROM profile')

我想知道如何更新tkinter中组合框下的下拉列表。 这就是我目前拥有的:

from tkinter import *
from tkinter import ttk
import sqlite3

#profile system
def prof_value_input():
    conn = sqlite3.connect('data.db')
    c = conn.cursor()

    query = c.execute('SELECT prof FROM profile')
    prof = []
    for row in c.fetchall():
        prof.append(row)
    return prof

    c.close()
    conn.close()

def prof_add_btn():
    conn = sqlite3.connect('data.db')
    c = conn.cursor()

    name=prof_input.get()
    newProf = c.execute('INSERT INTO profile VALUES (:prof)',{'prof':name})
    prof_input.set('')

    conn.commit()
    conn.close()
    return 

prof_add_btn = Button(menu, text='add profile', width=10, command=prof_add_btn)
prof_input = ttk.Combobox(menu, width=30)
prof_input.grid(column=0, row=0)
prof_add_btn.grid(column=1, row=0)
prof_input['values'] = prof_value_input()
我真的被卡住了,不知道该怎么办。
基本上,我正在尝试创建一个配置文件系统,您可以在组合框中输入一个名称,然后它将在sqlite3数据库中更新,在此之后,我希望它使用添加到数据库中的新配置文件更新组合框。

简单。在这个例子中,我从这个

prof_input=Combobox(菜单,宽度=30)

要调用此函数,请调用下面的方法

self.cbox=Combobox(self,宽度=10,后命令=self.updtcblist)

使用后命令将使您能够回调以进行更新(甚至 每次显示菜单时,创建菜单

在此方法中将对此进行更新

def updtcblist(self):
    list = self.getPortLst()
    self.cbox['values'] = list

因此,在您的代码中设置一个后命令并为其设置函数名。

返回
内部
prof\u add\u btn()之前添加
prof\u input['values']+=(name,)


这段代码设法让它运行起来

OP没有使用类,那么
self
从何而来?这只是一个例子来说明它。在给出答案之前,我想确切地说明它是如何工作的。刚刚更新,以更好地澄清它。我知道你的课程进展如何,但是的,我没有使用课堂。是的,这一个似乎是,我相信我会有更多的时间,哈哈,请接受问题的答案,无论是这个还是@acw的答案。在后一种情况下,将其作为答案发布并接受。请参阅
prof_input['values']+=(name,)
之前的
return
内部
prof_add_btn()
。在将输入名称添加到表中之前,最好检查输入名称是否已经存在。哦,天哪,它工作了!!!非常感谢。最好将
prof.append(row)
更改为
prof.append(row[0])
因为
row
是一个包含一个元素的元组。