list = Listbox(self, relief=SUNKEN)
sbar.config(command=list.yview) # xlink sbar and list
list.config(yscrollcommand=sbar.set) # move one moves other
sbar.pack(side=RIGHT, fill=Y) # pack first=clip last
list.pack(side=LEFT, expand=YES, fill=BOTH) # list clipped first
pos = 0
for label in options: # add to listbox
list.insert(pos, label) # or insert(END,label)
pos += 1 # or enumerate(options)
#list.config(selectmode=SINGLE, setgrid=1) # select,resize modes
list.bind('
self.listbox = list
def runCommand(self, selection): # redefine me lower
print('You selected:', selection)
if name == 'main':
options = (('Lumberjack-%s' % x) for x in range(20)) # or map/lambda, [...]
ScrolledList(options).mainloop()
This module can be run standalone to experiment with these widgets, but it is also
designed to be useful as a library object. By passing in different selection lists to the
options argument and redefining the runCommand method in a subclass, the Scrolled
List component class defined here can be reused anytime you need to display a scrol-
lable list. In fact, we’ll be reusing it this way in Chapter 11’s PyEdit program. With just
a little forethought, it’s easy to extend the tkinter library with Python classes this way.
When run standalone, this script generates the window in Figure 9-14, shown here
with Windows 7 look-and-feel. It’s a Frame, with a Listbox on its left containing 20
generated entries (the fifth has been clicked), along with an associated Scrollbar on its
right for moving through the list. If you move the scroll, the list moves, and vice versa.
Figure 9-14. scrolledlist at the top
Listboxes and Scrollbars| 523