Advertisement
deadmarshal

2listbox

Jun 18th, 2015
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.53 KB | None | 0 0
  1. import Tkinter
  2.  
  3.  
  4. class Application(Tkinter.Frame):
  5.     def __init__(self, master):
  6.         Tkinter.Frame.__init__(self, master)
  7.         self.master.minsize(width=512, height=256)
  8.         self.master.config()
  9.         self.pack()
  10.  
  11.         self.main_frame = Tkinter.Frame()
  12.         self.main_frame.pack(fill='both', expand=True)
  13.  
  14.         self.data = {
  15.             'Toyota': ['Camry', 'Corolla', 'Prius'],
  16.             'Ford': ['Fusion', 'Focus', 'Fiesta'],
  17.             'Volkswagen': ['Passat', 'Jetta', 'Beetle'],
  18.             'Honda': ['Accord', 'Civic', 'Insight']
  19.         }
  20.  
  21.         self.make_listbox = Tkinter.Listbox(self.main_frame)
  22.         self.make_listbox.pack(fill='both', expand=True, side=Tkinter.LEFT)
  23.  
  24.         # here we bind the make listbox selection to our method
  25.         self.make_listbox.bind('<<ListboxSelect>>', self.load_models)
  26.  
  27.         self.model_listbox = Tkinter.Listbox(self.main_frame)
  28.         self.model_listbox.pack(fill='both', expand=True, side=Tkinter.LEFT)
  29.  
  30.         # insert our items into the list box
  31.         for i, item in enumerate(self.data.keys()):
  32.             self.make_listbox.insert(i, item)
  33.  
  34.     def load_models(self, *args):
  35.         selection = self.make_listbox.selection_get()
  36.  
  37.         # clear the model listbox
  38.         self.model_listbox.delete(0, Tkinter.END)
  39.  
  40.         # insert the models into the model listbox
  41.         for i, item in enumerate(self.data[selection]):
  42.             self.model_listbox.insert(i, item)
  43.  
  44. root = Tkinter.Tk()
  45. app = Application(root)
  46. app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement