Web Analytics Made Easy - Statcounter
Skip to content

Listbox

Listbox call reference

The standard listbox like you'll find in most GUIs. Note that the return values from this element will be a list of results, not a single result. This is because the user can select more than 1 item from the list (if you set the right mode).

layout = [[sg.Listbox(["1", "2", "3", "4", "5"], size=(10, 2))]]

Events

A Listbox can generate 3 different events, each will return the key of the element as the event. There are 2 controls for these 3 event types:

  1. If the parameter enable_events is True then clicking an item in the list will generate an event.
  2. If the parameter bind_return_key is True then these 2 events are enabled:
    A. If the return key is pressed when the Listbox has focus
    B. If an item is double-clicked

Values Dictionary

The values dictionary entry for a Listbox is a list of the currently selected items. This list will be composed of the original items you passed in as the values parameter. If objects were supplied, then the objects chosen will be in the list. This is an important detail.

The List of values

The list of items to be chosen from can be many different types, including specific object. In the window, the list is shown using the __str__ method. If you cast a float 3.14 to a string, then it will be "3.14". It will appear of course the same when printed/changed into a string. When the entry is returned in the values dictionary it will have the original float value of 3.14, not the string "3.14".

This allows you to more easily implement applications in multiple languages. The visual representation of the item is decoupled from the identification of that item.

Selection Modes

Some elements, such as Listbox, Table, Tree, enable uses to select multiple items. The select_mode parameter determines how the multiple selection is handled. For example, maybe you only want 1 item at a time to be chosen, or want the selection to work like file selection does in Explorer where the control and shift keys help with selection. The valid values for this parameter are specified using these "constants" (variables)

  • LISTBOX_SELECT_MODE_SINGLE - Only 1 item can be selected
  • LISTBOX_SELECT_MODE_MULTIPLE - Multiple items and can drag mouse to select
  • LISTBOX_SELECT_MODE_BROWSE - Multiple items
  • LISTBOX_SELECT_MODE_EXTENDED - Multiple items. Works much like Explorer

Size

The size parameter determines the width in characters to show and the height in lines of text. Thus a size of (20, 10) will be 20 characters wide and show 10 items. If you have more than the specified height then scrollbars are added to your Listbox.