gpt4 book ai didi

python - 根据组合框 1 填充组合框 2

转载 作者:行者123 更新时间:2023-11-29 16:03:19 27 4
gpt4 key购买 nike

我有 2 个组合框。但我需要根据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没有改变。

最佳答案

主要问题是:

  1. 您错误地初始化了combo_maker
  2. 您通过了以下结果 comb_input_type() 而不是函数引用 combo_make.bind(...) 中的 comb_input_type

以下是根据您的修改后的代码:

#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)

关于python - 根据组合框 1 填充组合框 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55964708/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com