Python data will not input into CSV file
<p>I am working on a project for my shop that would allow me to track dimensions for my statistical process analysis.  I have a part with 2 dimensions that I will measure 5 samples for.  The dimension are OAL (Over All Length) and a Barb Diameter.  I got Python and tKinter to create the window and put all the data into the correct place, but it will not export to the CSV file.  It keeps telling me that the name is not defined, but the variable does exist and if I use a print command, the correct value comes up in the shell.  So I know the variable exists  I'm not sure if it's because I'm using tKinter or not.  Any help would be appreciated.</p>

<pre><code>import time
from tkinter import *
import threading
import csv
import datetime


def gui():
    root = Tk()
    root.title("Troy Screw Products")

    titleLabel = Label(root,text="Inspection Worksheet")
    partNumLabel = Label(root,text="Part #68800")
    now = datetime.datetime.now()

    typeLabel = ["Barb Dia","Barb Dia","OAL","Knurl","Threads","Chamfer","OD","OD","OD"]
    dimLabel = [".356",".333",".437",".376","n/a",".258",".337",".321",".305"]
   tolLabel = [".354/.358",".331/.335",".433/.441",".374/.378","1/4-20",".252/.263",".335/.339",".319/.323",".303/.307"]
    observations = ["Obs 1","Obs 2","Obs 3","Obs 4","Obs 5"]
    cd1Obs = []

    Label(text="Inspection Worksheet").grid(row=0,column=0)
    Label(text="Part #68800").grid(row=1,column=0)

    r=0
    for c in typeLabel:
             Label(text=c,relief=RIDGE,width=15).grid(row=2,column=r)
             r=r+1
    r=0            
    for c in dimLabel:
            Label(text=c,relief=RIDGE,width=15).grid(row=3,column=r)
            r=r+1

    r=0            
    for c in tolLabel:
            Label(text=c,relief=RIDGE,width=15).grid(row=4,column=r)
            r=r+1

    r=0            
    for c in tolLabel:
            Checkbutton(width=15).grid(row=5,column=r)
            r=r+1

    Label(text="").grid(row=6,column=1)
    Label(text="").grid(row=7,column=1)

    Label(text="OAL").grid(row=8,column=2)
    Label(text="Barb Dia").grid(row=8,column=6)

    r=9            
    for c in observations:
            Label(text=c,width=15).grid(row=r,column=1)
            Label(text=c,width=15).grid(row=r,column=5)
            r=r+1

    dimOneOb1=StringVar()
    dimOneOb2=StringVar()
    dimOneOb3=StringVar()
    dimOneOb4=StringVar()
    dimOneOb5=StringVar()
    dimTwoOb1=StringVar()
    dimTwoOb2=StringVar()
    dimTwoOb3=StringVar()
    dimTwoOb4=StringVar()
    dimTwoOb5=StringVar()

    Entry(textvariable=dimOneOb1).grid(row=9,column=2)
    Entry(textvariable=dimOneOb2).grid(row=10,column=2)
    Entry(textvariable=dimOneOb3).grid(row=11,column=2)
    Entry(textvariable=dimOneOb4).grid(row=12,column=2)
    Entry(textvariable=dimOneOb5).grid(row=13,column=2)

    Entry(textvariable=dimTwoOb1).grid(row=9,column=6)
    Entry(textvariable=dimTwoOb2).grid(row=10,column=6)
    Entry(textvariable=dimTwoOb3).grid(row=11,column=6)
    Entry(textvariable=dimTwoOb4).grid(row=12,column=6)
    Entry(textvariable=dimTwoOb5).grid(row=13,column=6)

    def submitEntry():
        groupOal1=dimOneOb1.get()
        groupOal2=dimOneOb2.get()
        groupOal3=dimOneOb3.get()
        groupOal4=dimOneOb4.get()
        groupOal5=dimOneOb5.get()

        groupBarb1=dimTwoOb1.get()
        groupBarb2=dimTwoOb2.get()
        groupBarb3=dimTwoOb3.get()
        groupBarb4=dimTwoOb4.get()
        groupBarb5=dimTwoOb5.get()

        writeCsv()

    Button(text="Submit",command=submitEntry).grid(row=14,column=7)

    def writeCsv():
        with open("CD 68800 OAL.csv", "a") as cdOal: #open file and give file variable name; r=read, w=write, a=append
            cdOalWriter = csv.writer(cdOal) #Give writer a variable name
            cdOalWriter.writerow([now.strftime("%Y-%m-%d %H:%M"),groupOal1,groupOal2,groupOal3,groupOal4,groupOal5])
            csOal.close()



    root.mainloop()

op1 = threading.Thread(target = gui)
op1.start()
</code></pre>

<p>Simply pass those variables in the method, <code>writeCsv()</code>. Because the <em>groupOas</em> are local to the <code>submitEntry()</code> function, its called function, <code>writeCsv</code>, does not see such objects. Also, below uses the <a href="http://stackoverflow.com/questions/36901/what-does-double-star-and-star-do-for-python-parameters">argument unpack idiom</a>, <code>*</code> of list of objects (I comment out the more verbose lines):</p>

<pre><code>def submitEntry():
    groupOal1=dimOneOb1.get()
    groupOal2=dimOneOb2.get()
    groupOal3=dimOneOb3.get()
    groupOal4=dimOneOb4.get()
    groupOal5=dimOneOb5.get()
    group0as = [groupOal1,groupOal2,groupOal3,groupOal4,groupOal5]

    groupBarb1=dimTwoOb1.get()
    groupBarb2=dimTwoOb2.get()
    groupBarb3=dimTwoOb3.get()
    groupBarb4=dimTwoOb4.get()
    groupBarb5=dimTwoOb5.get()

    writeCsv(*group0as)
    # writeCsv(groupOal1,groupOal2,groupOal3,groupOal4,groupOal5)

def writeCsv(a, b, c, d, e):
    with open("CD 68800 OAL.csv", "a") as cdOal:
        cdOalWriter = csv.writer(cdOal)
        cdOalWriter.writerow([now.strftime("%Y-%m-%d %H:%M"), a, b, c, d, e])
        csOal.close()
</code></pre>

python csv tkinter