'Attempting to write data from PYSIMPLEGUI application to xlsx and it keeps throwing UnboundLocalError

def main(): window1, window2 = make_win1(), None # start off with 1 window open

while True:             # Event Loop
    window, event, values = sg.read_all_windows()
    if event == sg.WIN_CLOSED or event == 'Exit':
        window.close()
        if window == window2:       # if closing win 2, mark as closed
            window2 = None         
        elif window == window1:     # if closing win 1, exit program
            break
    elif event == 'Launch 2nd Window' and not window2:
        window2 = make_win2()
    if event == 'Submit':
        df = df.append(values, ignore_index = True)
        df.to_excel(EXCEL_FILE, index= False)
        sg.popup('Survey Submitted!')
window.close()

Error: UnboundLocalError: local variable 'df' referenced before assignment



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source