0

I have been making an app which lets the users check-boxes. Depending on what boxes they check it will display different information. I decided to use PySimpleGUI for this project. I made 6 check-boxes and one text input which I want the user to be able to choose between the check-boxes and enter a title of a movie in the text input box. Depending on what check-boxes they select it will display different information based on the movie whose title was entered in the text input.

  • When I try to process the title value entered in the text input it process all values including the boolean values of the check-boxes. The information my code tries to process is: {0: ;'my input', 'Title': True, 'Year': False...}. I only need to process the my input/the movie title input and not the boolean values of the check-boxes.

Here is an example of my code (for reference I am also using the IMDBPY library to search for movies (which I have made work, the problem is that the id = search[0].movieID line is processing too many values.):

         def run_code():
            global name
            while True:
                event, value = window.read()
                if event == 'SEARCH':
                    print(values)
                    name = str(values)[5:-2]
    
                print('Please wait while your results load...')
                search = ia.search_movie(name)
    
                id = search[0].movieID

            if values['Title'] == True:
                print(movie_title)

I am trying to make my code search for the ID of the film title which would be typed by the user in an input field and than (at the bottom) and print the movie title depending if they have the title checkbox selected. At this point I just get an error saying id = search[0].movieID IndexError: list index out of range) To my understanding id = search[0].movieID is taking too many values (which it is, it is taking in all the values, input and check-boxes) I only want it to take in the text input value.

How should I spread out the values to deal with this issue?

Jack
  • 44
  • 6

1 Answers1

0

Define keys in your sg elements. Then you can pick from values just the item that you need. For example:

def test():
    layout = [[sg.Text('Movie title:'), sg.Input(key='input')],
              [sg.Checkbox('Title', key='title'), sg.Checkbox('Reverse title', key='reverse')],
              [sg.Button('Search', enable_events=True), sg.Cancel()]
              ]
    window = sg.Window('My Window', layout, finalize=True)

    while True:
        event, values = window.read()
        print(f'event = {event}, values = {values}')
        if event in (sg.WINDOW_CLOSED, 'Cancel', 'Exit'):
            break
        if event == 'Search':
            print('Searching now...')
            if values['title']:
                print(f'Title = {values["input"]}')
            if values['reverse']:
                print(f'Reverse title = {values["input"][::-1]}')
            # any other action as needed

For example, when you check both the checkboxes and click on Search, you'll get

event = Search, values = {'input': 'abcde', 'title': True, 'reverse': True}
Searching now...
Title = abcde
Reverse title = edcba

Now you can be sure that you've got the right title as input by the user.

Except of this, you should check the value you get in search = ia.search_movie(name). For a title not found in the database, you'll probably get None and that's where id = search[0].movieID gets you the IndexError: list index out of range because there is no None[0]. So, you may want to add something like

if search is None:
    print('Not found!')
    continue
  • `enable_events=True` is not necessary in statement `sg.Button('Search', enable_events=True)`. `enable_events` used to turn on the element specific events. If this button is a target, should it generate an event when filled in. – Jason Yang Mar 22 '21 at 06:12