-1

When I want to get the index of a selected item in a listbox, and the listbox is empty i get a error.

window['Listbox'].get_indexes()[0]
------------------------------------
IndexError: tuple index out of range

The original list that I use in my program is not empty, but it's changing so it may be empty and in that case when I press on the listbox the program crashes.

Code:

import PySimpleGUI as sg

list1 = []

layout = [[sg.Listbox(list1, s=(13, 6), enable_events=True, key='Listbox')]]

window = sg.Window("listbox test 1", layout=layout, size=(100, 100))


while True:
    event, values = window.read()
    if event == "Exit" or event == sg.WIN_CLOSED:
        break
    if event == 'Listbox':
        print(window['Listbox'].get_indexes()[0])

Is there maybe a simple fix for that?

If no, then I'd have to add a check if the listbox is empty or no.

Eskimo868
  • 238
  • 1
  • 12
  • Why are you calling get? The value is in the values dictionary. values['Listbox'] has the currently selected values. if values['Listbox']: is all you need to check for no entries. Keep it simple. – Mike from PSG Nov 25 '21 at 17:45
  • Won't values['Listbox'] give me back a string? (The element from the list) I am looking for the intex. – Eskimo868 Nov 25 '21 at 17:48
  • ok, then you can look it up based on the original choices or you can use get too. The bigger point is that, In general, always check lengths when performing indexing into something. – Mike from PSG Nov 25 '21 at 17:51

2 Answers2

1

May this work in your case?

if event == 'Listbox':
   try:
      print(window['Listbox'].get_indexes()[0])
   except:
      print("Empty list")
ma4strong
  • 300
  • 2
  • 8
1

All that's required is an if statement.

if values['Listbox']:    # if something was selected
   first_entry = values['Listbox'][0]
   window['Listbox'].get_indexes()[0]  # if you want the index... it'll be safe because values says there are entries


Mike from PSG
  • 5,312
  • 21
  • 39
  • If i try to: print(values['Listbox'][0]) when the listbox is empty the program crashes and i get this error: IndexError: list index out of range So this unfortunetly does not solve my problem. – Eskimo868 Nov 25 '21 at 18:03
  • Which is why you must use an if-statement to check to see if empty. The first statement, the if statement, is significant and the whole point of the explanation. It's a core concept kind of thing. The try works because you still get the error. There still an error in the code. The try simply catches it. – Mike from PSG Nov 25 '21 at 18:15
  • Okay, i tried to check if the listbox is empty by doing this: if values['Listbox'][0] == '': But i still get a error. – Eskimo868 Nov 25 '21 at 18:21
  • Actually, it should be `if values['Listbox'] == []:`, and it is the same as `if values['Listbox']:`. If you have a list or a tuple, you can index it only if it is not an empty list or an empty tuple. `[][0]` will get exception `IndexError: list index out of range`. – Jason Yang Nov 25 '21 at 18:30
  • The truthiness will be fine for just `values['Listbox']` if that's not clear enough, then `if len(values['Listbox']):` will do it. The bigger point, issue, problem, is that [0] can NEVER be done unless an if or some check is added first. – Mike from PSG Nov 25 '21 at 18:35
  • It's an easy test to see that an empty list is a boolean false. if []: print('Never going to run this') else: print('EMPTY') – Mike from PSG Nov 25 '21 at 18:37
  • Eskimo - I'm not giving up on you and this problem.... The reason you're getting errors every time you try adding [0] is because you MUST check the length first. Your comment that said ` i tried to check if the listbox is empty by doing this: if values['Listbox'][0] ` is a good example of the problem. The answer above shows how to check the length. That's what my if statement does. NOTICE that there is no [0] in that if statement. You must check the length before attempting to perform an index lookup. Your if statement's bug was the addition of [0]. – Mike from PSG Nov 25 '21 at 19:24