pysimplegui – Python unable to see the file path that the user picked

I continually keep getting the error NameError: name ‘filename’ not defined. I tried making filename a global variable but it still is not able to see it. I continually keep getting the error NameError: name ‘filename’ not defined. I tried making filename a global variable but it still is not able to see it. I continually keep getting the error NameError: name ‘filename’ not defined. I tried making filename a global variable but it still is not able to see it. I continually keep getting the error NameError: name ‘filename’ not defined. I tried making filename a global variable but it still is not able to see it. I continually keep getting the error NameError: name ‘filename’ not defined. I tried making filename a global variable but it still is not able to see it.

import PySimpleGUI as sg
import time
import csv
from pathlib import Path
import os

sg.theme('DarkBlue')

binFile1=binFile2=binFile3=binFile4=binFile5=binFile6=binFile7=binFile8 = None
binFile9=binFile10=binFile11=binFile12=binFile13=binFile14=binFile15=binFile16 = None

layouts = [
    [   # Layout 0
        [sg.Text('How many driver bits do you need? (Enter a number between 1 and 71)')],
        [sg.Input(key='-IN0-', expand_x=True, enable_events=True)],
        [sg.Text('', size=(20, 1), key='-OUTPUT0-')],
        [sg.Button('< Prev', disabled=True, key='Prev 0'), sg.Push(), sg.Button('Next >', key='Next 0')]],
    [   # Layout 1
        [sg.Text('Choose .CSV File')],
        [sg.Input(key='-IN1-', expand_x=True), sg.FileBrowse(file_types=(("CSV Files (.csv)", "*.csv"),))],
        [sg.Text('', size=(20, 1), key='-OUTPUT1-')],
        [sg.Button('< Prev', key='Prev 1'), sg.Push(), sg.Button('Submit', key='Next 1')]],
    [   # Layout 2
        [sg.Text('Please choose where you would like to export the .BIN files...')],
        [sg.Input(key='-IN2-', expand_x=True), sg.FolderBrowse()],
        [sg.Text('', size=(20, 1), key='-OUTPUT2-')],
        [sg.Button('< Prev', key='Prev 2'), sg.Push(), sg.Button('Next >', key='Next 2')]],
    [   # Layout 3
        [sg.Text('Please go back and verify your settings. nIf everything looks correct, click Write!.')],
        [sg.Text('', size=(20, 1), key='-OUTPUT3-')],
        [sg.Button('< Prev', key='Prev 3'), sg.Push(), sg.Button('Write!', key='Next 3')]],
    [   # Layout 4
        [sg.Text('Compilation Complete! nYour files have been saved in your chosen directory.')],
        [sg.Text('Finish !', expand_x=True, justification='center')],
        [sg.Push(), sg.Button('Exit')]],
]

layout = [[sg.Column(item, visible=(i==0), key=f'Column {i}') for i, item in enumerate(layouts)]]
window = sg.Window('CSV to Binary', layout, enable_close_attempted_event=True)
column = 0

def open_output_files(num_bytes):
    global binFile1, binFile2, binFile3, binFile4, binFile5, binFile6, binFile7, binFile8, 
           binFile9, binFile10, binFile11, binFile12, binFile13, binFile14, binFile15, binFile16
    if num_bytes == 1:
        binFile1 = open("157.bin", "wb+")
        return binFile1
    elif num_bytes == 2:
        binFile1 = open("157.bin", "wb+")
        binFile2 = open("158.bin", "wb+")
        return binFile1, binFile2
    elif num_bytes == 3:
        binFile1 = open("157.bin", "wb+")
        binFile2 = open("158.bin", "wb+")
        binFile3 = open("159.bin", "wb+")
        return binFile1, binFile2, binFile3
    elif num_bytes == 4:
        binFile1 = open("157.bin", "wb+")
        binFile2 = open("158.bin", "wb+")
        binFile3 = open("159.bin", "wb+")        
        binFile4 = open("160.bin", "wb+")
        return binFile1, binFile2, binFile3, binFile4
    elif num_bytes == 5:
        binFile1 = open("157.bin", "wb+")
        binFile2 = open("158.bin", "wb+")
        binFile3 = open("159.bin", "wb+")        
        binFile4 = open("160.bin", "wb+")
        binFile5 = open("161.bin", "wb+")
        return binFile1, binFile2, binFile3, binFile4, binFile5

    elif num_bytes == 6:
        binFile1 = open("157.bin", "wb+")
        binFile2 = open("158.bin", "wb+")
        binFile3 = open("159.bin", "wb+")        
        binFile4 = open("160.bin", "wb+")
        binFile5 = open("161.bin", "wb+")  
        binFile6 = open("162.bin", "wb+")        
        return binFile1, binFile2, binFile3, binFile4, binFile5, binFile6

    elif num_bytes == 7:
        binFile1 = open("157.bin", "wb+")
        binFile2 = open("158.bin", "wb+")
        binFile3 = open("159.bin", "wb+")        
        binFile4 = open("160.bin", "wb+")
        binFile5 = open("161.bin", "wb+")  
        binFile6 = open("162.bin", "wb+")             
        binFile7 = open("163.bin", "wb+")
        return binFile1, binFile2, binFile3, binFile4, binFile5, binFile6, binFile7

    elif num_bytes == 8:
        binFile1 = open("157.bin", "wb+")
        binFile2 = open("158.bin", "wb+")
        binFile3 = open("159.bin", "wb+")        
        binFile4 = open("160.bin", "wb+")
        binFile5 = open("161.bin", "wb+")  
        binFile6 = open("162.bin", "wb+")             
        binFile7 = open("163.bin", "wb+")       
        binFile8 = open("164.bin", "wb+")
        return binFile1, binFile2, binFile3, binFile4, binFile5, binFile6, binFile7, binFile8

    elif num_bytes == 9:
        binFile1 = open("157.bin", "wb+")
        binFile2 = open("158.bin", "wb+")
        binFile3 = open("159.bin", "wb+")
        binFile4 = open("160.bin", "wb+")
        binFile5 = open("161.bin", "wb+")
        binFile6 = open("162.bin", "wb+")        
        binFile7 = open("163.bin", "wb+")
        binFile8 = open("164.bin", "wb+")        
        binFile9 = open("165.bin", "wb+")
        return binFile1, binFile2, binFile3, binFile4, binFile5, binFile6, binFile7, binFile8, binFile9

    elif num_bytes == 10:
        binFile1 = open("157.bin", "wb+")
        binFile2 = open("158.bin", "wb+")
        binFile3 = open("159.bin", "wb+")
        binFile4 = open("160.bin", "wb+")
        binFile5 = open("161.bin", "wb+")
        binFile6 = open("162.bin", "wb+")        
        binFile7 = open("163.bin", "wb+")
        binFile8 = open("164.bin", "wb+")        
        binFile9 = open("165.bin", "wb+")     
        binFile10 = open("166.bin", "wb+")
        return binFile1, binFile2, binFile3, binFile4, binFile5, binFile6, binFile7, binFile8, binFile9, binFile10

    elif num_bytes == 11:
        binFile1 = open("157.bin", "wb+")
        binFile2 = open("158.bin", "wb+")
        binFile3 = open("159.bin", "wb+")
        binFile4 = open("160.bin", "wb+")
        binFile5 = open("161.bin", "wb+")
        binFile6 = open("162.bin", "wb+")        
        binFile7 = open("163.bin", "wb+")
        binFile8 = open("164.bin", "wb+")        
        binFile9 = open("165.bin", "wb+")
        binFile10 = open("166.bin", "wb+")        
        binFile11 = open("167.bin", "wb+")
        return binFile1, binFile2, binFile3, binFile4, binFile5, binFile6, binFile7, binFile8, binFile9, binFile10, binFile11

    elif num_bytes == 12:
        binFile1 = open("157.bin", "wb+")
        binFile2 = open("158.bin", "wb+")
        binFile3 = open("159.bin", "wb+")
        binFile4 = open("160.bin", "wb+")
        binFile5 = open("161.bin", "wb+")
        binFile6 = open("162.bin", "wb+")        
        binFile7 = open("163.bin", "wb+")
        binFile8 = open("164.bin", "wb+")        
        binFile9 = open("165.bin", "wb+")
        binFile10 = open("166.bin", "wb+")        
        binFile11 = open("167.bin", "wb+")
        binFile12 = open("168.bin", "wb+")
        return binFile1, binFile2, binFile3, binFile4, binFile5, binFile6, binFile7, binFile8, binFile9, binFile10, binFile11, binFile12

    elif num_bytes == 13:
        binFile1 = open("157.bin", "wb+")
        binFile2 = open("158.bin", "wb+")
        binFile3 = open("159.bin", "wb+")
        binFile4 = open("160.bin", "wb+")
        binFile5 = open("161.bin", "wb+")
        binFile6 = open("162.bin", "wb+")        
        binFile7 = open("163.bin", "wb+")
        binFile8 = open("164.bin", "wb+")        
        binFile9 = open("165.bin", "wb+")
        binFile10 = open("166.bin", "wb+")        
        binFile11 = open("167.bin", "wb+")
        binFile12 = open("168.bin", "wb+")
        binFile13 = open("169.bin", "wb+")
        return binFile1, binFile2, binFile3, binFile4, binFile5, binFile6, binFile7, binFile8, binFile9, binFile10, binFile11, binFile12, binFile13

    elif num_bytes == 14:
        binFile1 = open("157.bin", "wb+")
        binFile2 = open("158.bin", "wb+")
        binFile3 = open("159.bin", "wb+")
        binFile4 = open("160.bin", "wb+")
        binFile5 = open("161.bin", "wb+")
        binFile6 = open("162.bin", "wb+")        
        binFile7 = open("163.bin", "wb+")
        binFile8 = open("164.bin", "wb+")        
        binFile9 = open("165.bin", "wb+")
        binFile10 = open("166.bin", "wb+")        
        binFile11 = open("167.bin", "wb+")
        binFile12 = open("168.bin", "wb+")
        binFile13 = open("169.bin", "wb+")
        binFile14 = open("170.bin", "wb+")
        return binFile1, binFile2, binFile3, binFile4, binFile5, binFile6, binFile7, binFile8, binFile9, binFile10, binFile11, binFile12, binFile13, binFile14

    elif num_bytes == 15:
        binFile1 = open("157.bin", "wb+")
        binFile2 = open("158.bin", "wb+")
        binFile3 = open("159.bin", "wb+")
        binFile4 = open("160.bin", "wb+")
        binFile5 = open("161.bin", "wb+")
        binFile6 = open("162.bin", "wb+")        
        binFile7 = open("163.bin", "wb+")
        binFile8 = open("164.bin", "wb+")        
        binFile9 = open("165.bin", "wb+")
        binFile10 = open("166.bin", "wb+")        
        binFile11 = open("167.bin", "wb+")
        binFile12 = open("168.bin", "wb+")
        binFile13 = open("169.bin", "wb+")
        binFile14 = open("170.bin", "wb+")
        binFile15 = open("171.bin", "wb+")
        return binFile1, binFile2, binFile3, binFile4, binFile5, binFile6, binFile7, binFile8, binFile9, binFile10, binFile11, binFile12, binFile13, binFile14, binFile15

    else:
        binFile1 = open("157.bin", "wb+")
        binFile2 = open("158.bin", "wb+")
        binFile3 = open("159.bin", "wb+")
        binFile4 = open("160.bin", "wb+")
        binFile5 = open("161.bin", "wb+")
        binFile6 = open("162.bin", "wb+")        
        binFile7 = open("163.bin", "wb+")
        binFile8 = open("164.bin", "wb+")        
        binFile9 = open("165.bin", "wb+")
        binFile10 = open("166.bin", "wb+")        
        binFile11 = open("167.bin", "wb+")
        binFile12 = open("168.bin", "wb+")
        binFile13 = open("169.bin", "wb+")
        binFile14 = open("170.bin", "wb+")
        binFile15 = open("171.bin", "wb+")
        binFile16 = open("172.bin", "wb+")
        return binFile1, binFile2, binFile3, binFile4, binFile5, binFile6, binFile7, binFile8, binFile9, binFile10, binFile11, binFile12, binFile13, binFile14, binFile15, binFile16



def show(step):
    global column
    window[f'Column {column}'].update(visible=False)
    column += step
    window[f'Column {column}'].update(visible=True)
    window.refresh()
    window.move_to_center()

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSE_ATTEMPTED_EVENT:
        if column in (0, 5):
            break
        else:
            show(-1)
    elif event in ('Exit', 'Finish!'):
        break
    elif event.startswith("Prev"):
        show(-1)
    elif event.startswith("Next"):
        show(+1)
    elif event == 'Top':
        show(-4)
    
    if event == sg.WIN_CLOSED:
        break
    elif event == 'Submit':
       global filename
       filename = open('-IN1-')
        
if event == 'Write!':           
    start_time = time.time()        
    init = True    
    linecnt = 0
    numfiles = int(values('-IN0-' % 8))
    open_output_files(numfiles) # MODIFY THIS TO BE # OF BURN IN CHANNELS / 8 (Round up!!!)
    filename = open('-IN1-')
    
    
linecnt = 0    
with open(os.path.join(filename), "rb") as vecfile:
    vectors = csv.reader(vecfile, delimiter=",")
    for vec in vectors:
        if(linecnt < 0):
            next
        else: 
            temp_vec=""
            for i in range(0,72):
                temp_vec += vec[i]
                #print(temp_vec)
                
            temp_vec = temp_vec.translate({ord(x): '0' for x in ['Z', 'X', 'H', 'L']})
            vec_str = temp_vec + ''  #ADD PADDED 0'S TO MAKE OVERALL NUMBER OF VECTORS % 8 = 0
            #print(vec_str)
            vec_str = vec_str[::-1]
            #print(vec_str)
            #print(linecnt, vec_str[:8])# for debug
            
            byte1 = int(vec_str[7::-1],2) # COMMENT OR UNCOMMENT # OF BYTES REQUIRED FOR BURNIN SETUP
            byte2 = int(vec_str[15:7:-1],2)
            byte3 = int(vec_str[23:15:-1],2)
            byte4 = int(vec_str[31:23:-1],2)
            byte5 = int(vec_str[39:31:-1],2)
            byte6 = int(vec_str[47:39:-1],2)
            byte7 = int(vec_str[55:47:-1],2)
            byte8 = int(vec_str[63:55:-1],2)
            byte9 = int(vec_str[71:63:-1],2)
            byte10 = int(vec_str[79:71:-1],2)
            byte11 = int(vec_str[87:79:-1],2)
            byte12 = int(vec_str[95:87:-1],2)
            byte13 = int(vec_str[103:95:-1],2)
            byte14 = int(vec_str[111:103:-1],2)
            byte15 = int(vec_str[119:111:-1],2)
            byte16 = int(vec_str[127:119:-1],2)
            
                #print(vec_str, file = logFile)
         
            binFile1.write(byte1.to_bytes(1, byteorder="big")) # COMMENT OR UNCOMMENT # OF BYTES REQUIRED FOR BURNIN SETUP
            binFile2.write(byte2.to_bytes(1, byteorder="big"))
            binFile3.write(byte3.to_bytes(1, byteorder="big"))
            binFile4.write(byte4.to_bytes(1, byteorder="big"))
            binFile5.write(byte5.to_bytes(1, byteorder="big"))
            binFile6.write(byte6.to_bytes(1, byteorder="big"))
            binFile7.write(byte7.to_bytes(1, byteorder="big"))
            binFile8.write(byte8.to_bytes(1, byteorder="big"))
            binFile9.write(byte9.to_bytes(1, byteorder="big"))
            binFile10.write(byte10.to_bytes(1, byteorder="big"))
            binFile11.write(byte11.to_bytes(1, byteorder="big"))
            binFile12.write(byte12.to_bytes(1, byteorder="big"))
            binFile13.write(byte13.to_bytes(1, byteorder="big"))
            binFile14.write(byte14.to_bytes(1, byteorder="big"))
            binFile15.write(byte15.to_bytes(1, byteorder="big"))
            binFile16.write(byte16.to_bytes(1, byteorder="big"))
            
            linecnt += 1

    binFile1.close() # COMMENT OR UNCOMMENT # OF BYTES REQUIRED FOR BURNIN SETUP
    binFile2.close()
    binFile3.close()
    binFile4.close()
    binFile5.close()
    binFile6.close()
    binFile7.close()
    binFile8.close()
    binFile9.close()
    binFile10.close()
    binFile11.close()
    binFile12.close()
    binFile13.close()
    binFile14.close()
    binFile15.close()
    #binFile16.close()

window.close()

Leave a Comment