2016-07-11 62 views
-1

我想從用戶輸入數據,並把它轉換成文本文件,但如下有一個錯誤:Tkinter的追蹤誤差3.5.1窗口8.1

Exception in Tkinter callback 
Traceback (most recent call last): 
    File "C:\Users\dasom\AppData\Local\Programs\Python\Python35-32\Lib\tkinter\__init__.py", line 1549, in __call__ 
    return self.func(*args) 
    File "C:/Users/dasom/PycharmProjects/Exercise/4.pyw", line 5, in save_data 
    filed.write("Depot:\n%s\n" % depot.get()) 
AttributeError: 'NoneType' object has no attribute 'get'` 

它在說,大約1549線init .py文件中,我查找了它,並且我不明白問題是什麼。

def __call__(self, *args): 
    """Apply first function SUBST to arguments, than FUNC.""" 
    try: 
     if self.subst: 
      args = self.subst(*args) 
     return self.func(*args) 
    except SystemExit: 
     raise 
    except: 
     self.widget._report_exception() 

這裏是我的全部代碼

from tkinter import * 

def save_data(): 
    filed = open("deliveries.txt", "a") 
    filed.write("Depot:\n%s\n" % depot.get()) 
    filed.write("Description :\n%s\n" % description.get()) 
    filed.write("Address :\n%s\n" % address.get("1.0", END)) 
    depot.delete(0, END) 
    description.delete(0, END) 
    address.delete("1.0", END) 

app = Tk() 
app.title('Head-Ex Deliveries') 

Label(app, text='Depot:').pack() 
depot = Entry(app).pack() 

Label(app, text="Description:").pack() 
description = Entry(app).pack() 

Label(app, text='Address:').pack() 
address = Text(app).pack() 

Button(app, text='save', command=save_data).pack() 

app.mainloop() 

其實,我只是打字教科書的代碼。對你的幫助表示感謝。謝謝。

回答

1

如果教科書有這樣的代碼,這是一本很差的教科書。此行:

depot = Entry(app).pack() 

正在做兩件事。首先它會創建一個Entry,然後將其放入應用程序中。不幸的是,pack()方法就地執行並返回None,而不是對原始的Entry小部件的引用。拆分起來:

depot = Entry(app) 
depot.pack() 

做到這一點從就地方法,你希望指向一個有用的對象的引用分配None返回值的所有類似情況。

+0

謝謝老虎! – DasomJung