2016-09-21 39 views
0

我正在爲我的商店開發一個項目,該項目允許我跟蹤統計過程分析的維度。我有2個維度的一部分,我將測量5個樣本。尺寸是OAL(全長)和倒鉤直徑。我得到了Python和tKinter來創建窗口並將所有數據放入正確的位置,但不會導出到CSV文件。它一直告訴我該名稱沒有定義,但該變量確實存在,如果我使用打印命令,shell中會顯示正確的值。所以我知道變量存在我不確定是否因爲我正在使用tKinter。任何幫助,將不勝感激。Python數據不會輸入到CSV文件

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() 
+0

請出示實際的錯誤在Tkinter的回調 回溯(最近通話最後一個) –

+0

>>>例外: 返回self.func(* args) 文件「C:\ Users \ Marc \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ tkinter \ __ init__.py」,第1549行,在__call__中\ Marc \ Documents \ SPC Data \ Inspection Database \ 68800 Inspection.py「,第92行,在submitEntry writeCsv() 文件」C:\ Users \ Marc \ Documents \ SPC Data \ Inspection Database \ 68800 Inspection.py「,第99行,寫入Csv cdOalWriter.writerow([now.strftime(「%Y-%m-%d%H:%M」),groupOal1,groupOal2,groupOal3,groupOal4,groupOal5]) NameError:name'groupOal1'is not defined – shark38j

+0

please在問題中包含錯誤。作爲評論是不可讀的。你的問題應該有一個你可以點擊的「編輯」鏈接。 –

回答

0

只需在方法writeCsv()中傳遞這些變量即可。因爲groupOas對於submitEntry()函數是本地的,所以它的被調用函數writeCsv不會看到這樣的對象。另外,下面使用argument unpack idiom*對象名單,(我註釋掉了更詳細的線路):

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() 
+0

感謝您的幫助。如果我在def和submitEntry部分中列出writeCsv函數中的變量,它將起作用。我無法將* group0解包工作。這似乎是問題在* items命令中。 「項目」沒有變量,那麼該如何工作?我真的很想知道這應該如何工作。 – shark38j

+0

哎呀,不要在'def()'中重新打包,只需枚舉包裝條件即可。另外,在傳遞參數時,您不需要保留確切的名稱。請參閱編輯。 – Parfait