首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >按照combobox 1填充combobox 2

按照combobox 1填充combobox 2
EN

Stack Overflow用户
提问于 2019-05-03 06:56:55
回答 2查看 130关注 0票数 0

我有2 ComboBox。但是我需要根据combo_type的值更新combo_maker。

我试着做一个ComboSelected,但那是个失败。

当combobox_maker选择任何值时。combobox_type没有变化。我尝试了很少的代码改变,但什么也没有。

代码语言:javascript
运行
复制
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没有变化。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-05-03 08:14:44

主要问题是:

  1. 您错误地初始化了combo_maker
  2. 您将传递comb_input_type()的结果,而不是comb_input_typecombo_make.bind(...)中的函数引用。

以下是基于您的代码的修改代码:

代码语言:javascript
运行
复制
#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)
票数 0
EN

Stack Overflow用户

发布于 2019-05-03 07:45:43

我无法运行您的代码,因为您正在从DB中提取数据,但是通常您可以为此创建dict。下面是一个简单的样本。

代码语言:javascript
运行
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55964708

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档