Search code examples
pythontkintercombobox

More than one combobox


I choose the list name on the first ComboBox. Can't display elements on second combobox. (etc. x=[1,2,3] - y=[4,5,6,7] - z=[8,9] If x is selected on the first combobox, I want to see the x elements on the second combobox.)

İÇECEKLER = ['Su', 'Maden Suyu', 'Çay', 'Kahve', 'Nescafe', 'Kola', 'Fanta', 'Sprite']
YİYECEKLER = ['Simit', 'Poğaça', 'Açma', 'Boyoz', 'Tahinli', 'Kumru', 'Tost', 'Kumpir']
TATLILAR = ['Sütlaç', 'Puding', 'Aşure', 'Revani', 'Pasta', 'Künefe', 'Waffle', 'Tiramisu']
DİĞER = ['KDV', 'Servis Bedeli']

value0 = StringVar ()
value1 = StringVar ()
value2 = StringVar ()
value3 = StringVar ()

KategoriKutu = Frame (root, width = 1000, height = 300, bd = 16, relief = 'raise')
KategoriKutu.pack (side = TOP)
Kategori = Frame (KategoriKutu, width = 800, height = 300, bd = 8, relief = 'raise')
Kategori.pack (side = LEFT)

KategoriSeç = ttk.Combobox (Kategori, textvariable = value0, state = 'readonly', width = 16, justify = "center", font = ('arial', 30, 'bold'))
KategoriSeç ['values'] = ('', 'İÇECEKLER', 'YİYECEKLER', 'TATLILAR', 'DİĞER')
KategoriSeç.current()
KategoriSeç.grid (row = 0, column = 0)

ÜrünSeç = ttk.Combobox (Kategori, textvariable = value1, state = 'readonly', width = 16, justify = "center", font = ('arial', 30, 'bold'))
ÜrünSeç ['values'] = ('', )
ÜrünSeç.current()
ÜrünSeç.grid (row = 1, column = 0)

root.mainloop()

This is the spot that confused my mind.

a = ['Su', 'Maden Suyu', 'Çay', 'Kahve', 'Nescafe', 'Kola', 'Fanta', 'Sprite']
b = ['Simit', 'Poğaça', 'Açma', 'Boyoz', 'Tahinli', 'Kumru', 'Tost', 'Kumpir']
c = ['Sütlaç', 'Puding', 'Aşure', 'Revani', 'Pasta', 'Künefe', 'Waffle', 'Tiramisu']
d = ['KDV', 'Servis Bedeli']

I have a Total of four lists, and they all have different elements.

a_combo = ttk.Combobox(Kategori, state = 'readonly',width = 16, justify = "center", font = ('arial', 30, 'bold'))
a_combo ['values'] = ('', 'a', 'b', 'c', 'd')
a_combo.grid (row = 0, column = 0)
a_combo.bind("<<ComboboxSelected>>",lambda e: set_next_combo(e,b_combo,a))

b_combo = ttk.Combobox(Kategori, state = 'readonly', width = 16, justify = "center", font = ('arial', 30, 'bold'))
b_combo.grid (row = 1, column = 0)

If I chose which list on the first ComboBox, I failed to fetch the list elements I selected on the second ComboBox.

if first combobox is 'a' selected, second combobox 'a' elements or 'b' selected 'b' elements or 'c' selected 'c' elements need to arrive.


Solution

  • Just create a dictionary out of your lists and have your <<ComboboSelected>> event call the corresponding list.

    from tkinter import *
    from tkinter import ttk
    
    root = Tk()
    test_dict = {
    "a" : ['Su', 'Maden Suyu', 'Çay', 'Kahve', 'Nescafe', 'Kola', 'Fanta', 'Sprite'],
    "b" : ['Simit', 'Poğaça', 'Açma', 'Boyoz', 'Tahinli', 'Kumru', 'Tost', 'Kumpir'],
    "c" : ['Sütlaç', 'Puding', 'Aşure', 'Revani', 'Pasta', 'Künefe', 'Waffle', 'Tiramisu'],
    "d" : ['KDV', 'Servis Bedeli']}
    
    KategoriKutu = Frame (root, width = 1000, height = 300, bd = 16, relief = 'raise')
    KategoriKutu.pack (side = TOP)
    Kategori = Frame (KategoriKutu, width = 800, height = 300, bd = 8, relief = 'raise')
    Kategori.pack (side = LEFT)
    
    def set_next_combo(event,widget):
        result = test_dict.get(a_combo.get(),[])
        widget["values"] = result
        widget.set(result[0] if result else " ")
    
    a_combo = ttk.Combobox(Kategori, state = 'readonly',width = 16, justify = "center", font = ('arial', 30, 'bold'))
    a_combo ['values'] = ('', 'a', 'b', 'c', 'd')
    a_combo.grid (row = 0, column = 0)
    a_combo.bind("<<ComboboxSelected>>",lambda e: set_next_combo(e,b_combo))
    
    b_combo = ttk.Combobox(Kategori, state = 'readonly', width = 16, justify = "center", font = ('arial', 30, 'bold'))
    b_combo.grid (row = 1, column = 0)
    
    root.mainloop()