【问题标题】:Fill combox 2 according to combobox 1根据组合框1填充组合框2
【发布时间】:2019-05-03 06:56:55
【问题描述】:

我有 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 没有变化。

【问题讨论】:

    标签: python mysql tkinter combobox


    【解决方案1】:

    主要问题是:

    1. 你错误地初始化了combo_maker
    2. 你传递的结果是 comb_input_type() 代替函数引用 comb_input_typecombo_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)
    

    【讨论】:

    • combo_maker['value'] 有问题。我有价值观,但有 {}。像 {test 1}、{test2}。它只是没有 {} 的“test 1”。也许对于那个 combo_type 不起作用。
    • 我认为首先将组合框值展平以获得结果。在我之前的一篇文章中,为什么combo_maker = ttk.Combobox(Product,value=[item for result in combo_input_maker() for item in result if item],state="readonly")
    • 由于结果中有空格,您需要使用列表推导填充结果:result=[rec[0] for rec in cursor] in combo_input_maker()。与combo_input_type() 中的类似。
    • some_name 在查询中是 [some_name]。我想编辑。但他们已经在身边了。谢谢你。
    【解决方案2】:

    我无法运行您的代码,因为您正在从数据库中提取数据,但通常您可以为此创建 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()
    

    【讨论】:

    • 适用于 dic。但我在值中与 {} 有同样的问题
    猜你喜欢
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    • 2014-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多