Search code examples
pythontkintercombobox

Multiple Tkinter Comboboxes where the displayed values depend on the user-choice in the other boxes


Lets say I got the following dictionary:

dict = {'A': [1,2,3], 'B': [4,5,6]}

With this dictionary and two Comboboxes the user may get a certain combination, e.g. A-1. The problem is that choices like A-5 or B-1 shouldn't be possible to choose.

Thus I thought about the following way to achieve this. If the user chooses 'A' in the first box then the options in the 2nd box have to be 1,2 and 3. If he chooses 'B' then the displayed options are 4, 5 and 6. The user can confirm his combination (A-1,...) by pressing a button at the end.

A little example what I meant.

enter image description here

Thanks in advance!


Solution

  • So here is my proposed code:

    from tkinter import Tk, Button, Frame, StringVar
    from tkinter.ttk import Combobox
    
    
    options = {'A': [1, 2, 3], 'B': [4, 5, 6]}
    
    
    def get_var_1(event):
        value = cb1_var.get()
        cb2_var.set(options[value][0])
        cb2.config(values=options[value])
    
    
    def get_info():
        print(cb1_var.get(), cb2_var.get())
    
    
    root = Tk()
    
    cb_frame = Frame(root)
    cb_frame.pack(side='left')
    
    cb1_values = list(options.keys())
    
    cb1_var = StringVar()
    cb1_var.set(cb1_values[0])
    cb1 = Combobox(cb_frame, values=list(options.keys()), textvariable=cb1_var)
    cb1.pack(side='top')
    cb1.bind('<<ComboboxSelected>>', get_var_1)
    
    
    cb2_var = StringVar()
    cb2_var.set(options[cb1_values[0]][0])
    cb2 = Combobox(cb_frame, values=options[cb1_values[0]], textvariable=cb2_var)
    cb2.pack(side='bottom')
    
    
    btn_frame = Frame(root)
    btn_frame.pack(side='right')
    Button(btn_frame, text='Confirm', command=get_info).pack()
    
    
    root.mainloop()
    

    Basically it should be mainly understandable but if You have questions ask them.