Pmw.ScrolledListBox

Name

Pmw.ScrolledListBox() - listbox with optional scrollbars

Inherits

Pmw.MegaWidget

Description

A scrolled listbox consists of a standard listbox widget with optional scrollbars which can be used to scroll the listbox. The scrollbars can be dynamic, which means that a scrollbar will only be displayed if it is necessary. That is, if the listbox does not contain enough entries, the vertical scrollbar will be automatically hidden and if the entries are not wide enough, the horizontal scrollbar will be automatically hidden.

Options

Options for this megawidget and its base classes are described below.

dblclickcommand
This specifies a function to call when mouse button 1 is double clicked over an entry in the listbox component. The default is None.

hscrollmode
The horizontal scroll mode. If 'none', the horizontal scrollbar will never be displayed. If 'static', the scrollbar will always be displayed. If 'dynamic', the scrollbar will be displayed only if necessary. The default is 'dynamic'.

items
Initialisation option. A tuple containing the initial items to be displayed by the listbox component. The default is ().

labelmargin
Initialisation option. If the labelpos option is not None, this specifies the distance between the label component and the rest of the megawidget. The default is 0.

labelpos
Initialisation option. Specifies where to place the label component. If not None, it should be a concatenation of one or two of the letters 'n', 's', 'e' and 'w'. The first letter specifies on which side of the megawidget to place the label. If a second letter is specified, it indicates where on that side to place the label. For example, if labelpos is 'w', the label is placed in the center of the left hand side; if it is 'wn', the label is placed at the top of the left hand side; if it is 'ws', the label is placed at the bottom of the left hand side.

If None, a label component is not created. The default is None.

scrollmargin
Initialisation option. The distance between the scrollbars and the listbox widget. The default is 2.

selectioncommand
This specifies a function to call when mouse button 1 is single clicked over an entry in the listbox component or if the <Space> or <Return> key is hit while the listbox has focus. The default is None.

usehullsize
Initialisation option. If true, the size of the megawidget is determined solely by the width and height options of the hull component.

Otherwise, the size of the megawidget is determined by the width and height of the listbox component, along with the size and/or existence of the other components, such as the label, the scrollbars and the scrollmargin option. All these affect the overall size of the megawidget. The default is 0.

vscrollmode
The vertical scroll mode. If 'none', the vertical scrollbar will never be displayed. If 'static', the scrollbar will always be displayed. If 'dynamic', the scrollbar will be displayed only if necessary. The default is 'dynamic'.

Components

Components created by this megawidget and its base classes are described below.

horizscrollbar
The horizontal scrollbar. By default, this component is a Tkinter.Scrollbar. Its component group is Scrollbar.

hull
This acts as the body for the entire megawidget. Other components are created as children of the hull to further specialise this class. By default, this component is a Tkinter.Frame.

label
If the labelpos option is not None, this component is created as a text label for the megawidget. See the labelpos option for details. Note that to set, for example, the text option of the label, you need to use the label_text component option. By default, this component is a Tkinter.Label.

listbox
The listbox widget which is scrolled by the scrollbars. By default, this component is a Tkinter.Listbox.

vertscrollbar
The vertical scrollbar. By default, this component is a Tkinter.Scrollbar. Its component group is Scrollbar.

Methods

Only methods specific to this megawidget are described below. For a description of its inherited methods, see the manual for its base class Pmw.MegaWidget. In addition, methods from the Tkinter.Listbox class are forwarded by this megawidget to the listbox component.

bbox(index)
This method is explicitly forwarded to the listbox component's bbox() method. Without this explicit forwarding, the bbox() method (aliased to grid_bbox()) of the hull would be invoked, which is probably not what the programmer intended.

clear()
Delete all items from the scrolled listbox. Equivalent to setlist(()).

get(first = None, last = None)
This is the same as the get() method of the listbox component, except that if first is None all list elements are returned.

getcurselection()
Same as getvalue() method.

getvalue()
Return a list of the currently selected items of the listbox.

setlist(items)
Replace all the items of the listbox component with those specified by the items sequence.

setvalue(textOrList)
Set the current selection for the scrolled list to textOrList.

If textOrList is a string, select only the list item specified.

Otherwise, select only the list items specified by textOrList, which must be a sequence of strings.

size()
This method is explicitly forwarded to the listbox component's size() method. Without this explicit forwarding, the size() method (aliased to grid_size()) of the hull would be invoked, which is probably not what the programmer intended.

Example

The image at the top of this manual is a snapshot of the window (or part of the window) produced by the following code.

class Demo:
    def __init__(self, parent):
        # Create the ScrolledListBox.
        self.box = Pmw.ScrolledListBox(parent,
                items=('Sydney', 'Melbourne', 'Brisbane'),
                labelpos='nw',
                label_text='Cities',
                listbox_height = 6,
                selectioncommand=self.selectionCommand,
                dblclickcommand=self.defCmd,
                usehullsize = 1,
                hull_width = 200,
                hull_height = 200,
        )

        # Create a group widget to contain the scrollmode options.
        w = Pmw.Group(parent, tag_text='Scroll mode')
        w.pack(side = 'bottom', padx = 5, pady = 5)

        hmode = Pmw.OptionMenu(w.interior(),
                labelpos = 'w',
                label_text = 'Horizontal:',
                items = ['none', 'static', 'dynamic'],
                command = self.sethscrollmode,
                menubutton_width = 8,
        )
        hmode.pack(side = 'top', padx = 5, pady = 5)
        hmode.invoke('dynamic')

        vmode = Pmw.OptionMenu(w.interior(),
                labelpos = 'w',
                label_text = 'Vertical:',
                items = ['none', 'static', 'dynamic'],
                command = self.setvscrollmode,
                menubutton_width = 8,
        )
        vmode.pack(side = 'top', padx = 5, pady = 5)
        vmode.invoke('dynamic')

        buttonBox = Pmw.ButtonBox(parent)
        buttonBox.pack(side = 'bottom')
        buttonBox.add('yview', text = 'Show\nyview', command = self.showYView)
        buttonBox.add('scroll', text = 'Page\ndown', command = self.pageDown)
        buttonBox.add('center', text = 'Center', command = self.centerPage)

        # Pack this last so that the buttons do not get shrunk when
        # the window is resized.
        self.box.pack(fill = 'both', expand = 1, padx = 5, pady = 5)

        # Do this after packing the scrolled list box, so that the
        # window does not resize as soon as it appears (because
        # alignlabels has to do an update_idletasks).
        Pmw.alignlabels((hmode, vmode))

        # Add some more entries to the listbox.
        items = ('Andamooka', 'Coober Pedy', 'Innamincka', 'Oodnadatta')
        self.box.setlist(items)
        self.box.insert(2, 'Wagga Wagga', 'Perth', 'London')
        self.box.insert('end', 'Darwin', 'Auckland', 'New York')
        index = list(self.box.get(0, 'end')).index('London')
        self.box.delete(index)
        self.box.delete(7, 8)
        self.box.insert('end', 'Bulli', 'Alice Springs', 'Woy Woy')
        self.box.insert('end', 'Wallumburrawang', 'Willandra Billabong')

    def sethscrollmode(self, tag):
        self.box.configure(hscrollmode = tag)

    def setvscrollmode(self, tag):
        self.box.configure(vscrollmode = tag)

    def selectionCommand(self):
        sels = self.box.getcurselection()
        if len(sels) == 0:
            print 'No selection'
        else:
            print 'Selection:', sels[0]

    def defCmd(self):
        sels = self.box.getcurselection()
        if len(sels) == 0:
            print 'No selection for double click'
        else:
            print 'Double click:', sels[0]

    def showYView(self):
        print self.box.yview()

    def pageDown(self):
        self.box.yview('scroll', 1, 'page')

    def centerPage(self):
        top, bottom = self.box.yview()
        size = bottom - top
        middle = 0.5 - size / 2
        self.box.yview('moveto', middle)

Pmw 1.2 - 5 Aug 2003 - Home
Manual page last reviewed: 30 August 1998