-1

I want to get an input data from user and put it into text file, but there's an error as follows:

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'`

It says about line 1549 in init.py file, I looked up for it and I coudn't understand what the problem is.

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

Here's my whole code

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

Actually, I just typed the code of textbook. Your help will be greatly appreciated. Thanks.

plamut
  • 3,085
  • 10
  • 29
  • 40
DasomJung
  • 31
  • 1
  • 4

1 Answers1

1

If the textbook has code like that, it's a poor textbook. This line:

depot = Entry(app).pack()

is doing two things. First it creates an Entry, and then it places it into the app. Unfortunately, the pack() method acts in-place and returns None instead of a reference to the original Entry widget. Split it up:

depot = Entry(app)
depot.pack()

Do this for all similar instances of assigning the None return value from an in-place method to a reference that you expect to point to a useful object.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97