pythonpysimplegui

how to resolve InputText key error in this simple PySimpleGUI script


I do not see why i am getting this error: Traceback (most recent call last): File ....\play_download.py", line 55, in url = values['-url-'] ~~~~~~^^^^^^^^^ KeyError: '-url-'

This error appears when I click on radio button download_video_with_yt_dlp enter image description here

    import PySimpleGUI as sg
    from my_scripts import *
    from my_constants import *
    import sys
    import glob
    import yt_dlp
    
    sg.theme_background_color('#3d5c5c')
    sg.set_options(font=('Fira Code', 16))
    l1 = sg.Text('Put url here',font=('Fira Code', 16), expand_x=True, justification='center')
    l2 = sg.Text('Put 1 for Chef_Max_Mariola, 2 for JuliaCrossBow, 3 for French_Vibes, 4 for Studio_Italiano_InClasse, 5 for D:', 
        font=('Fira Code', 16), expand_x=True, justification='center')
    t1 = sg.Text('', enable_events=True, font=('Fira Code', 16), expand_x=True, justification='left')
    t2 = sg.Text('', enable_events=True, font=('Fira Code', 16), expand_x=False, justification='left')
    
    def delete_video_in_D():
            list_of_files = [os.remove(p)  for p in glob.glob(r'D:\\*.*') if (os.path.isfile(p) and is_video(p))]
            
    
    layout1 = [
        [sg.Radio(key, 'Radio', enable_events=True, key=key) for key in ['Chef_Max_Mariola', 'JuliaCrossBow', 
        'French_Vibes', 'Studio_Italiano_InClasse', 'download_video_with_yt_dlp']]]
    
    layout2 = [
        [sg.Radio(key, 'Radio', enable_events=True, key=key) for key in ['Chef_Max_Mariola', 'JuliaCrossBow', 
        'French_Vibes', 'Studio_Italiano_InClasse', 'download_video_with_yt_dlp']], [l1],[t1,  sg.InputText(key='-url-')], [l2], [t2, sg.InputText(key='-folder-')]]
    
    
    FOLDERS = {'1:Chef_Max_Mariola_PATH',
               '2:JuliaCrossBow_PATH',
               '3:French_Vibes_PATH',
               '4:Studio_Italiano_InClasse_PATH',
               '5:r"D:\\"'}
    
    
    window = sg.Window('Main Window', layout1)
    
    while True:
    
        event, values = window.read()
    
        if event == sg.WIN_CLOSED:
            break
        elif event == 'Chef_Max_Mariola':
            play_videos_from_folder(Chef_Max_Mariola_PATH)
        elif event == 'JuliaCrossBow':
            play_videos_from_folder(JuliaCrossBow_PATH)
        elif event == 'French_Vibes':
            play_videos_from_folder(French_Vibes_PATH)
        elif event == 'Studio_Italiano_InClasse':
            play_videos_from_folder(Studio_Italiano_InClasse_PATH)
        elif event == 'download_video_with_yt_dlp':
            delete_video_in_D()
            window = sg.Window('Main Window', layout2)
            url = values['-url-']
            folder = values['-folder-']
            os.chdir(FOLDERS[folder])
            try:
                with yt_dlp.YoutubeDL() as ydl:
                    ydl.download(link)
                list_of_files = glob.glob('*.*') 
                latest_file = max(list_of_files, key=os.path.getctime)
                # print("Download is completed successfully")
                # print(latest_file)
                new_name = re.sub(r'\s*\[.*?\]\s*', '', latest_file )
                os.rename(latest_file, new_name)
                proc = subprocess.Popen([r'C:\\Program Files\\VideoLAN\\VLC\\vlc.exe', new_name], close_fds=True)
                time.sleep(2)
            except Exception as e:
                print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
    
    
    window.close()


  [1]: https://i.sstatic.net/VCoJEVQt.png

Solution

  • The values was read from the window with layout1, not the window with layout2, so there's no element with key '-url-', also no key '-folder-'.

    The window with layout2 was initialized, not finalized, so you won't see it.

    Example Code

    import webbrowser
    import PySimpleGUI as sg
    
    def popup():
        layout = [
            [sg.Input(key='-url-')],
            [sg.Push(), sg.Button("OK"), sg.Button("Cancel")],
        ]
        window = sg.Window("URL", layout)
        while True:
            event, values = window.read()
            if event in (sg.WIN_CLOSED, "Cancel"):
                result = None
                break
            elif event == "OK":
                result = values['-url-'].strip()
                break
        window.close()
        return result
    
    layout = [[sg.Button("Download", size=20)]]
    window = sg.Window("Download", layout)
    while True:
        event, values = window.read()
        if event == sg.WIN_CLOSED:
            break
        elif event == "Download":
            url = popup()
            if url:
                webbrowser.open(url)
    window.close()