0

I'm working on a tkinter project that has 2 comboboxes, sourced with a dictionary.

The first one have to receive the key of the dictionary as choices.

The second one will receive the values of the selected key as choices.

Here is the code:

from tkinter import *

from tkinter import ttk as ttk

import resources #dictionary file

root=Tk()

root.geometry("600x600")


label=Label(root, text="cbb1")

label.grid(row=1, column=1)

key=[i for i in resources.Dico.keys()]

cbb = ttk.Combobox(root, values=key)

cbb.grid(row=1, column=2)

def callback(event):
    selected_key=cbb.get()
    return selected_key


cbb.bind('<<<ComboboxSelected>>>', callback)


label2=Label(root, text="Cbb2")

label2.grid(row=2, column=1)

cbb2=ttk.Combobox(root, values=[i for i in resources.Dico[selected_key]])

cbb2.grid(row=2, column=2)

root.mainloop()

Source example:

Dico={}

Dico={"valeur":("motif1", "motif2","motif3","motif4"),
      "valeur2":("test1","test2","test3")}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Does this answer your question: [tkinter-how-to-update-second-combobox-automatically-according-this-combobox](https://stackoverflow.com/questions/40126449/tkinter-how-to-update-second-combobox-automatically-according-this-combobox) – stovfl Nov 19 '19 at 23:46
  • Sorry, the question have been cut by the edit. I can’t manage with second cbb. When i change the value of the first one, it stay stuck on the base value i set to the first one. I need your help to link the second one in case of change of the first. – tiedien Nov 20 '19 at 06:02
  • @stovfl thank you, but i i really need to use dictionary, cause the first cbb will receive something like 20 keys for 100 values for the second one. It gonna be a pain and make it unreadable to do such a long conditional bloc, no ? – tiedien Nov 20 '19 at 06:14
  • ***"really need to use dictionary"***: You need a `sequence`, it is unrelated which type of is it. Your first `Combobox` uses `values=Disco.keys()`, your second uses `cbb2['values'] = Disco[cbb.get()]`. But, first you have to understand [Event-driven programming](https://stackoverflow.com/a/9343402/7414759), read also [Is this bad programming practice in tkinter?](https://stackoverflow.com/questions/25454065/is-this-bad-programming-practice-in-tkinter) and [Best way to structure a tkinter application](https://stackoverflow.com/a/17470842/7414759) – stovfl Nov 20 '19 at 08:26
  • 1
    It works ! I just had to use the config méthod in my callback to set the cbb.get() value in my second cbb. My issue was that i didn't know it's possible to use the config méthode to change the value of a widget after it's creation. Thanks a lot, for all you advices. – tiedien Nov 20 '19 at 14:14

0 Answers0