简体   繁体   English

Python:如何根据另一个组合框的选择从SQL获取ttk组合框的数据

[英]Python: How can I fetch data from SQL for a ttk combobox based on the selection of another combobox

I am trying to have two ttk comboboxes that fetch data from a sqlite database. 我试图有两个从sqlite数据库中获取数据的ttk组合框。 I managed to be able and list items in one combobox named divisiondropmenu using the combo_division() function. 我设法使用combo_division()函数在一个名为dividedropmenu的组合框中列出了项目。 Now I am trying to do a similar thing for the second combobox named productdropmenu, but the list would need to be narrowed down based on the first selected item in combobox named divisiondropmenu. 现在,我正在尝试对名为productdropmenu的第二个组合框执行类似的操作,但是需要根据名为dividedropmenu的组合框中的第一个选定项来缩小列表的范围。

For the SQL query to pass the needed information for the second combobox I was thinking of something of this sort: 为了使SQL查询传递第二个组合框所需的信息,我在想这样的事情:

'SELECT * FROM productname WHERE divisionname=?', (function_name())

Code for the two comboboxes so far: 到目前为止的两个组合框代码:

from tkinter import *
from tkinter import ttk
from tkinter.ttk import *
import sqlite3


def combo_division():
    conn = sqlite3.connect('anglingdatabase.db')
    cursor = conn.cursor()
    cursor.execute('SELECT divisionname FROM productdivision')
    data = []
    for row in cursor.fetchall():
    data.append(row[0])
    return data

root = Tk()
root.title("Random Window Title")
root.geometry('1280x720')

rows = 0
while rows < 50:
    root.rowconfigure(rows, weight=1)
    root.columnconfigure(rows, weight=1)
    rows += 1


divisiondropmenu = ttk.Combobox(root)
divisiondropmenu['values'] = combo_division()
divisiondropmenu.bind('<<ComboboxSelected>>', selecteddivision)
divisiondropmenu.grid(row=1, column=2, sticky='NESW')

productdropmenu = ttk.Combobox(root)
productdropmenu['values'] = combo_product()
productdropmenu.grid(row=2, column=2, sticky='NESW')

root.mainloop()
data = combo_division()
example_sql = ''' SELECT * FROM tablename WHERE division in ('{}') '''.format("','".join(data))

You could build the actual SQL dynamically provided that the data was never user input (to avoid SQL injection). 您可以动态构建实际的SQL,前提是该数据永远不要由用户输入(以避免SQL注入)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM