我有2 ComboBox。但是我需要根据combo_type的值更新combo_maker。
我试着做一个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_type发生变化时,combo_maker没有变化。
发布于 2019-05-03 08:14:44
主要问题是:
combo_maker
;comb_input_type()
的结果,而不是comb_input_type
在combo_make.bind(...)
中的函数引用。以下是基于您的代码的修改代码:
#COMBO MAKER
def combo_input_maker():#<--Populate combo_maker
# use global cursor instead of local one
cursor.execute("SELECT maker_name FROM makers")
result=[rec[0] for rec in cursor]
return result
#COMBO MAKER
#COMBO TYPE
def combo_input_type(event=None):#<--Populate combo_type
some_name = combo_maker.get()#<--Query with value select from combo_maker
# use global cursor
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 = [rec[0] for rec in cursor]
# populate combo_type
combo_type['value'] = result
combo_type.current(0)
return result
#COMBO TYPE
# connect DB once
conn = connect_db()
cursor = conn.cursor()
combo_maker = ttk.Combobox(Product,state="readonly")
combo_maker['value'] = combo_input_maker() #[item for result in combo_input_maker() for item in result if item]
combo_maker.bind('<<ComboboxSelected>>', combo_input_type) # use function reference here
combo_maker.place(x=5, y=5, height = 25, width = 180)
combo_type = ttk.Combobox(Product,state="readonly")
combo_type.place(x=200, y=5, height = 25, width = 180)
发布于 2019-05-03 07:45:43
我无法运行您的代码,因为您正在从DB中提取数据,但是通常您可以为此创建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()
https://stackoverflow.com/questions/55964708
复制相似问题