PySimpleGUI - ListBox Element



This GUI element in PySimpleGUI toolkit is a container that can display one or more items, and select from it. You can specify the number of items that can be visible at a time. If the number of items or their length becomes more than the dimensions of the Listbox, a vertical and/or horizontal scrollbar appears towards the right or bottom of the element.

Important properties of the ListBox class are as follows −

Sr.No. Property & Description
1 Values

List of values to display. Can be any type including mixed types

2 default_values

Which values should be initially selected

3 select_mode

Select modes are used to determine if only 1 item can be selected or multiple and how they can be selected.

4 no_scrollbar

Controls if a scrollbar should be shown. If True, no scrollbar will be shown

5 horizontal_scroll

Controls if a horizontal scrollbar should be shown. If True a horizontal scrollbar will be shown in addition to vertical

The "select_mode" property can have one of the following enumerated values −

  • LISTBOX_SELECT_MODE_SINGLE (default)

  • LISTBOX_SELECT_MODE_MULTIPLE

  • LISTBOX_SELECT_MODE_BROWSE

  • LISTBOX_SELECT_MODE_EXTENDED

The Listbox class inherits the update() method from the Element class. It effects changes in some of the properties when the window gets updated. The parameters to the update() method are −

Sr.No. Property & Description
1 Values

New list of choices to be shown to user

2 Disabled

Disable or enable state of the element

3 set_to_index

Highlights the item(s) indicated. If parm is an int, one entry will be set. If is a list, then each entry in the list is highlighted

4 scroll_to_index

Scroll the listbox so that this index is the first shown

5 select_mode

Changes the select mode

6 Visible

Control visibility of element

The Listbox element is in action in the following program. The PySimpleGUI window shows an Input element, a Listbox and the buttons with captions Add, Remove and Exit.

import PySimpleGUI as psg
names = []
lst = psg.Listbox(names, size=(20, 4), font=('Arial Bold', 14), expand_y=True, enable_events=True, key='-LIST-')
layout = [[psg.Input(size=(20, 1), font=('Arial Bold', 14), expand_x=True, key='-INPUT-'),
   psg.Button('Add'),
   psg.Button('Remove'),
   psg.Button('Exit')],
   [lst],
   [psg.Text("", key='-MSG-', font=('Arial Bold', 14), justification='center')]
]
window = psg.Window('Listbox Example', layout, size=(600, 200))
while True:
   event, values = window.read()
   print(event, values)
   if event in (psg.WIN_CLOSED, 'Exit'):
      break
   if event == 'Add':
      names.append(values['-INPUT-'])
      window['-LIST-'].update(names)
      msg = "A new item added : {}".format(values['-INPUT-'])
      window['-MSG-'].update(msg)
   if event == 'Remove':
      val = lst.get()[0]
      names.remove(val)
      window['-LIST-'].update(names)
      msg = "A new item removed : {}".format(val)
      window['-MSG-'].update(msg)
window.close()

Run the above code, type some text in the Input box and press Add button. The text will be added in the listbox below it.

Listbox Element

The get() method of the Listbox class returns the list of selected items. By default, a single item is selectable. The remove button gets the value of the selected item and removes it from the collection.

pysimplegui_element_class.htm
Advertisements