Search code examples
tkintercombobox

How to create Combobox array in tkinter?


I have an input n=5, so can I create 5 Combobox and combine them together to something like an array? I just want to get the value of each Combobox through its index i just like box[i].get().

Help me with this, please!


Solution

  • Here is what you can do.

    from tkinter import *
    from tkinter import ttk
    x=int(input("Enter number of Comboboxes: "))
    root=Tk()
    box_dict={}
    def change_icon( event, j):
        print("-------------------------")
        
        print ("Value: ",box_dict[j].get(),f"at Combobox: {box_dict[j]}")
    for i in range(x):
        
        Label(root,text=f"Combobox Number: {i}").pack()
        box=ttk.Combobox(root,state="readonly",values=['1','2','3','4'])
        box_dict[i] = box
        box.pack()
        box.bind("<<ComboboxSelected>>",lambda event, j=i: change_icon(event, j))
    
    root.mainloop()