To populate a Listbox based on the selection of another Listbox, you need to bind a method to the 1st Listbox's selection. Here's an example using makes/models of cars:
import Tkinter
class Application(Tkinter.Frame):
def __init__(self, master):
Tkinter.Frame.__init__(self, master)
self.master.minsize(width=512, height=256)
self.master.config()
self.pack()
self.main_frame = Tkinter.Frame()
self.main_frame.pack(fill='both', expand=True)
self.data = {
'Toyota': ['Camry', 'Corolla', 'Prius'],
'Ford': ['Fusion', 'Focus', 'Fiesta'],
'Volkswagen': ['Passat', 'Jetta', 'Beetle'],
'Honda': ['Accord', 'Civic', 'Insight']
}
self.make_listbox = Tkinter.Listbox(self.main_frame)
self.make_listbox.pack(fill='both', expand=True, side=Tkinter.LEFT)
# here we bind the make listbox selection to our method
self.make_listbox.bind('<<ListboxSelect>>', self.load_models)
self.model_listbox = Tkinter.Listbox(self.main_frame)
self.model_listbox.pack(fill='both', expand=True, side=Tkinter.LEFT)
# insert our items into the list box
for i, item in enumerate(self.data.keys()):
self.make_listbox.insert(i, item)
def load_models(self, *args):
selection = self.make_listbox.selection_get()
# clear the model listbox
self.model_listbox.delete(0, Tkinter.END)
# insert the models into the model listbox
for i, item in enumerate(self.data[selection]):
self.model_listbox.insert(i, item)
root = Tkinter.Tk()
app = Application(root)
app.mainloop()