不确定如何使用连接的数据库更新 python 中的组合框
Unsure how to update combobox in python with a database connected
我想弄清楚如何更新 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(menu, width=30)
到此调用下面的方法
self.cbox = Combobox(self, width = 10, postcommand = self.updtcblist)
Using a postcommand will enable you to callback to update (or even
create) the menu every time it is displayed.
在此方法中,这将被更新。
def updtcblist(self):
list = self.getPortLst()
self.cbox['values'] = list
因此在您的代码中设置一个后置命令并为其设置函数名称。
在 return
之前添加 prof_input['values'] += (name,)
prof_add_btn()
此代码成功运行
我想弄清楚如何更新 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(menu, width=30)
到此调用下面的方法
self.cbox = Combobox(self, width = 10, postcommand = self.updtcblist)
Using a postcommand will enable you to callback to update (or even create) the menu every time it is displayed.
在此方法中,这将被更新。
def updtcblist(self):
list = self.getPortLst()
self.cbox['values'] = list
因此在您的代码中设置一个后置命令并为其设置函数名称。
在 return
之前添加 prof_input['values'] += (name,)
prof_add_btn()
此代码成功运行