0

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.

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()
shark38j
  • 114
  • 13
  • please show the actual error – Bryan Oakley Sep 21 '16 at 21:39
  • >>> Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\Marc\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 1549, in __call__ return self.func(*args) File "C:\Users\Marc\Documents\SPC Data\Inspection Database\68800 Inspection.py", line 92, in submitEntry writeCsv() File "C:\Users\Marc\Documents\SPC Data\Inspection Database\68800 Inspection.py", line 99, in writeCsv cdOalWriter.writerow([now.strftime("%Y-%m-%d %H:%M"),groupOal1,groupOal2,groupOal3,groupOal4,groupOal5]) NameError: name 'groupOal1' is not defined – shark38j Sep 21 '16 at 21:57
  • please include the error in the question. It is unreadable as a comment. Your question should have an "edit" link that you can click on. – Bryan Oakley Sep 21 '16 at 22:35

1 Answers1

0

Simply pass those variables in the method, writeCsv(). Because the groupOas are local to the submitEntry() function, its called function, writeCsv, does not see such objects. Also, below uses the argument unpack idiom, * of list of objects (I comment out the more verbose lines):

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()
Community
  • 1
  • 1
Parfait
  • 104,375
  • 17
  • 94
  • 125
  • Thanks for the help. It works if I list out the variables in the writeCsv functions, both in the def and submitEntry portions. I couldn't get the *group0as unpack to work. It seems like the problem is in the *items command. There is no variable for "items" so how is that supposed to work? I would really like to understand how that is supposed to work. – shark38j Sep 22 '16 at 13:28
  • Whoops, do not pack again in `def()`, simply enumerate the packed terms. Also, when passing arguments, you do not need to keep exact names. See edit. – Parfait Sep 22 '16 at 23:18