我有2个ComboBox。但是我需要根据combo_maker的值来更新combo_type。
我试图进行ComboSelected筛选,但失败了。
当combobox_maker时,我选择了任何值。 combobox_type没有变化。我尝试了一些代码更改,但一无所获。
from tkinter import *
from tkinter import ttk
import sqlite3
import tkinter as tk
from tkinter import messagebox
import configparser
import tkinter
import tkinter
import mysql.connector
from mysql.connector import Error
Product = Tk()
Product.title ('App - Add Product')
Product.geometry("800x600")
#CONNECT DB
def connect_db():
config = configparser.ConfigParser()
config.read('config.ini')
return mysql.connector.connect(host = config['mysql']['host'],
port = config['mysql']['port'],
user = config['mysql']['database_user'],
passwd = config['mysql']['database_password'],
db = config['mysql']['database_name'])
#CONNECT DB
#COMBO MAKER
def combo_input_maker():#<--Populate combo_maker
conn = connect_db()
cursor = conn.cursor()
cursor.execute("SELECT maker_name FROM makers")
result=cursor.fetchall()
return result
#COMBO MAKER
#COMBO TYPE
def combo_input_type():#<--Populate combo_type
conn = connect_db()
cursor = conn.cursor()
some_name = combo_maker.get()#<--Query with value select from combo_maker
cursor.execute("SELECT t1.type FROM maker_types t1 INNER JOIN makers t2 ON t1.maker_id = t2.id WHERE maker_name =%s", [some_name])
result=cursor.fetchall()
return result
#COMBO TYPE
combo_maker = ttk.Combobox(Product,state="readonly")
combo_maker['value'] = [item for result in combo_input_maker() for item in result if item]
combo_maker.current(0)
combo_maker.bind('<<ComboboxSelected>>', combo_input_type())#<--When ComboboxSelected in combo_maker, call def combo_input_type()
combo_maker.place(x=5, y=5, height = 25, width = 180)
combo_type = ttk.Combobox(Product,state="readonly")
combo_type['value'] = [item for result in combo_input_type() for item in result if item]
combo_type.current(0)
combo_type.place(x=200, y=5, height = 25, width = 180)
Product.mainloop()
combo_maker更改后,combo_type不变。
答案 0 :(得分:0)
由于您要从数据库中提取数据,因此我无法运行您的代码,但是通常您可以为此创建dict
。下面是一个简单的示例。
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
dic = {"A": ["Apple","Orange"],
"B": ["Pineapple","Banana"],
"C": ["Watermelon","Kiwi"]}
def on_select(event):
b_box["values"] = dic[a_box.get()] #modify 2nd combobox directly
b_box.current(0)
a_box = ttk.Combobox(root,value=[k for k in dic],state="readonly")
a_box.pack()
b_box = ttk.Combobox(root,state="readonly")
b_box.pack()
a_box.bind("<<ComboboxSelected>>",on_select)
root.mainloop()
答案 1 :(得分:0)
主要问题是:
askdirectory
; combo_maker
代替的函数引用
comb_input_type()
中的comb_input_type
。下面是根据您的代码修改的代码:
combo_make.bind(...)