O'Reilly logo

Programming Python, 3rd Edition by Mark Lutz

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Listboxes and Scrollbars

Listbox widgets allow you to display a list of items for selection, and Scrollbars are designed for navigating through the contents of other widgets. Because it is common to use these widgets together, let's study them both at once. Example 10-9 builds both a Listbox and a Scrollbar, as a packaged set.

Example 10-9. PP3E\Gui\Tour\scrolledlist.py

from Tkinter import * class ScrolledList(Frame): def _ _init_ _(self, options, parent=None): Frame._ _init_ _(self, parent) self.pack(expand=YES, fill=BOTH) # make me expandable self.makeWidgets(options) def handleList(self, event): index = self.listbox.curselection( ) # on list double-click label = self.listbox.get(index) # fetch selection text self.runCommand(label) # and call action here def makeWidgets(self, options): # or get(ACTIVE) sbar = Scrollbar(self) 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 #list.config(selectmode=SINGLE, setgrid=1) # select,resize modes list.bind('<Double-1>', self.handleList) # set event handler self.listbox = list def runCommand(self, selection): # redefine me lower print 'You selected:', selection if _ _name_ _ == '_ _main_ _': options = map((lambda x: 'Lumberjack-' ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required