2016-07-11 5 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() 메서드는 내부에서 작동하고 원래 Entry 위젯에 대한 참조 대신 None을 반환합니다. 그것을 분할 :

depot = Entry(app) 
depot.pack() 

당신이 유용한 개체를 가리 키도록 기대 참조에에서 적절한 방법에서 None 반환 값을 할당 유사한 모든 인스턴스에 대해이 작업을 수행합니다.

+0

호랑이! – DasomJung