Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

Python tkinter bind Comboboxes together (CB 2 dependant on CB 1)

I am working with python tkinter and data tables in a csv file. What I'm trying to do is make the values of a second Combobox dependant on the entries of a first Combobox. The first CB (=eingabefeld_motor) displays the values of the csv file in column 0 (motorttnr). Upon selection of a valid motorttnr the second CB (eingabefeld_index) should then filter the relevant rows in my csv and display the values in column 2 (index). There may be up to 5 indexes per motorttnr (productIDs). The whole array is saved in "daten" not displayed here.

Here is what I have done so far:

def updatecombo(event=None):
    eingabe_motor = eingabefeld_motor.get()
    filter_motorttnr = filter(lambda a: eingabe_motor in a, daten)
    liste_filter_motorttnr = list(filter_motorttnr)
    eingabefeld_index["values"] = ttk.Combobox(root, value=liste_filter_motorttnr[0][2])
    eingabefeld_index.update()

eingabefeld_motor = ttk.Combobox(root, value=motorttnr[1:], state="readonly")
eingabefeld_motor.bind('<<ComboboxSelected>>', updatecombo)
eingabefeld_motor.grid(row=3, column=2, padx=20, pady=10, sticky="nsew")
eingabefeld_index = ttk.Combobox(root, state="readonly")
eingabefeld_index.grid(row=4, column=2, padx=20, pady=10, sticky="nsew")

What I'm getting as a result is shown in the picture. Unfortunately, there are no indexes in the second CB, just a String that's not included in my code.1 What can I do to fix this issue? Thank you in advance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Assume daten is a list of list, then updatecombo() should be as below:

def updatecombo(event=None):
    eingabe_motor = eingabefeld_motor.get()
    filter_motorttnr = filter(lambda a: eingabe_motor in a, daten)
    liste_filter_motorttnr = list(filter_motorttnr)
    eingabefeld_index["values"] = [x[1] for x in liste_filter_motorttnr]
    eingabefeld_index.set('') # clear selected

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...