from Tkinter import *

class ScrolledList(Frame):
  def __init__(self, options, parent=None):
    # Create a frame for the application
    Frame.__init__(self, parent)
    self.pack(expand=YES, fill=BOTH)
    # Create the widgets in the frame
    self.createWidgets(options)

  def handleList(self, event):
    index = self.listbox.curselection()
    label = self.listbox.get(index)
    self.runCommand(label)

  def createWidgets(self, options):
    # Create the Scrollbars
    sbary = Scrollbar(self)
    sbarx = Scrollbar(self)
    # Create the Listbox
    list = Listbox(self, relief=SUNKEN)
    # Setup for the Scrollbars
    sbary.config(command=list.yview)
    sbarx.config(command=list.xview, orient=HORIZONTAL)
    list.config(yscrollcommand=sbary.set, xscrollcommand=sbarx.set)
    # Make the widgets visible
    sbary.pack(side=RIGHT, fill=Y)
    sbarx.pack(side=BOTTOM, fill=X)
    list.pack(side=LEFT, expand=YES, fill=BOTH)
    # Add the items to the Listbox
    pos = 0
    for label in options:
      list.insert(pos, label)
      pos = pos + 1
    # Handle some keyboard events
    list.bind('<Double-1>', self.handleList)
    list.bind_all('<Up>', self.ListUp)
    list.bind_all('<Down>', self.ListDown)
    self.listbox = list

  def runCommand(self, selection):
    print 'You selected:', selection

  def ListUp(self, event):
    # KeyPress: up arrow key
    # Move the selection upwards
    index = self.listbox.curselection()
    number = self.listbox.index(index)
    if number > 0:
      self.listbox.select_clear(number)
      self.listbox.select_set(number - 1)
      self.listbox.see(number - 1)

  def ListDown(self, event):
    # KeyPress: down arrow key
    # Move the selection downwards
    index = self.listbox.curselection()
    number = self.listbox.index(index)
    if number < self.listbox.size() - 1:
      self.listbox.select_clear(number)
      self.listbox.select_set(number + 1)
      self.listbox.see(number + 1)

# Generate some data
options = map((lambda x: 'Sample Data - ' + str(x)), range(20))
# Create and run the application
ScrolledList(options).mainloop()