python tkinter reference in comboboxes created in for loop python tkinter reference in comboboxes created in for loop tkinter tkinter

python tkinter reference in comboboxes created in for loop


You can have your Application keep a dict object (or a list, but then the implementation is highly index-dependent), store your boxes in the dict with i as the key:

class Application(Frame):    def __init__(self, master):        Frame.__init__(self, master, relief="sunken", border=1)        # Various initialization code here        self.box_dict = {}    def create_widgets(self):        for i in range(9):            box = ttk.Combobox(self, state="readonly")            # Do various things with your box object here            self.box_dict[i] = box            # Only complication is registering the callback            box.bind("<<ComboboxSelected>>",                         lambda event, i=i: self.change_icon(event, i))    def change_icon(self, event, i):        self.var_Selected = self.box_dict[i].current()        print "The user selected value now is:"        print self.var_Selected

Then access the boxes via self.box_dict[0] etc.

Edit I made updates to the bind and change_icon method to have each box send its index number to change_icon when event is triggered.Edit2 Changed implementation to use dict instead of list, which seems more robust.