2

I am trying to make my GUI display information depending on the item chosen in the combobox. PySimpleGUI cookbook says that I should be using GetSelectedItemsIndexes() method, but when I try using it:

window.Element('_COMBOBOX_').GetSelectedItemsIndexes()

I get this:

AttributeError: 'Combo' object has no attribute 'GetSelectedItemsIndexes'

I tried type this into the console:

dir(window.Element('_COMBOBOX_'))

and it seems that GetSelectedItemsIndexes is not even there... So how can I get the index of a chosen value from combobox?

milka1117
  • 521
  • 4
  • 8
  • 17
  • The solution I found is to take the selected value from `value` list using the `key` of combobox :) – milka1117 Jul 18 '19 at 07:23
  • This is exactly the right way of getting the value. You do not need to make a call as ALL of the window's input values are returned to you when you call `window.Read()`. In fact, the function GetSelectedItemsIndexes is no longer available. – Mike from PSG Aug 10 '19 at 18:53

1 Answers1

6

This work for me:

import PySimpleGUI as sg

layout = [[sg.Combo(['choice 1', 'choice 2', 'choice 3'], enable_events=True, key='combo')],
          [sg.Button('Test'), sg.Exit()]
          ]

window = sg.Window('combo test', layout)

while True:
    event, values = window.Read()
    if event is None or event == 'Exit':
        break

    if event == 'Test':
        combo = values['combo']  # use the combo key
        print(combo)

window.Close()
kraka
  • 61
  • 2
  • You can also directly access the value by writing `values['combo']` – Mike from PSG Aug 10 '19 at 18:55
  • Oh, that is more simple. Thanks for the answer. – kraka Aug 11 '19 at 01:32
  • The examples in the documentation and all of the demo programs use the `values['key']` style lookups (that's as you pointed out, more simple). You may want to edit your answer to more closely match the designs of typical PySimpleGUI programs. – Mike from PSG Aug 12 '19 at 13:58