PySimpleGUI - Combo Element



The Combo element is a drop down list. It initially shows an Input element with an arrow towards its right hand side. When the arrow is clicked, the list box pulls down. So, you can enter text in the Input text box, or select an item from the drop down list, so that the selected item shows up in the Input box.

The Combo element functions more or less similarly to Listbox. It is populated by a collection of string items in a list. You can also specify the default value to be displayed on top.

Following are the important properties of the Combo class −

Sr.No. Property & Description
1 values

list of values to be displayed and to choose.

2 default_value

Choice to be displayed as initial value.

3 size

width, height. Width = characters-wide, height = the number of entries to show in the list.

The get() method returns the current (right now) value of the Combo. The update() method modifies following properties of the Combo object −

Sr.No. Property & Description
1 value

change which value is current selected based on new list of previous list of choices

2 values

change list of choices

3 set_to_index

change selection to a particular choice starting with index = 0

4 readonly

if True make element readonly (user cannot change any choices).

In the following example, we use the selection changed event of the Combo element. The selected element in the dropdown is removed if the user responds by pressing Yes on the Popup dialog.

import PySimpleGUI as psg
names = []
lst = psg.Combo(names, font=('Arial Bold', 14),  expand_x=True, enable_events=True,  readonly=False, key='-COMBO-')
layout = [[lst,
   psg.Button('Add', ),
   psg.Button('Remove'),
   psg.Button('Exit')],
   [psg.Text("", key='-MSG-',
      font=('Arial Bold', 14),
      justification='center')]
   ]
window = psg.Window('Combobox Example', layout, size=(715, 200))
while True:
   event, values = window.read()
   print(event, values)
   if event in (psg.WIN_CLOSED, 'Exit'):
      break
   if event == 'Add':
      names.append(values['-COMBO-'])
      print(names)
      window['-COMBO-'].update(values=names, value=values['-COMBO-'])
      msg = "A new item added : {}".format(values['-COMBO-'])
      window['-MSG-'].update(msg)
   if event == '-COMBO-':
      ch = psg.popup_yes_no("Do you want to Continue?", title="YesNo")
   if ch == 'Yes':
      val = values['-COMBO-']
      names.remove(val)
   window['-COMBO-'].update(values=names, value=' ')
   msg = "A new item removed : {}".format(val)
   window['-MSG-'].update(msg)
window.close()

When the Combo object emits the event (identified by its key "-COMBO-") as an item in the dropdown is clicked. A Yes-No popup is displayed asking for the confirmation. If the Yes button is clicked, the item corresponding to the text box of the Combo element is removed from the list and the element is repopulated by the remaining items.

A screenshot of the window is shown below −

Combo Element
pysimplegui_element_class.htm
Advertisements