-1

I'm working on building a simple, little text editor but I'm getting an error back that I just can't figure out. It has to do with my use of the .get() function in Tkinter. I'm using python2.7. Can anyone help? Below is the code:

from Tkinter import *


def open_file():
    user_file = open(user_input.get(), 'a')
    file_contents = user_file.read()
    text_box.insert(END, file_contents)
root = Tk()

user_input = Entry(root).pack(side=TOP)
text_box = Text(root).pack()
b1 = Button(root, text="open", command=open_file).pack(side=LEFT)
b2 = Button(root, text="save").pack(side=LEFT)
b3 = Button(root, text="quit", command=root.quit).pack(side=LEFT)

root.mainloop()

The error message reads:
Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1535, in __call__
    return self.func(*args)
  File "/home/brandon/TkProjects/TextEditor.py", line 7, in open_file
    user_file = open(user_input.get, 'a')
AttributeError: 'NoneType' object has no attribute 'get'

Which says that I don't have the attribute .get() but I don't understand why. I've created an Entry widget named user_input. I tried storing it in a variable but that didn't work either. Please help.

filename = user_input.get() 
Brandon
  • 321
  • 1
  • 4
  • 10
  • Read the error more closely. It doesn't say "you" don't have the attribute `.get()`, it's saying `NoneType` doesn't have the attribute. Ask yourself: why am I calling `.get()` on something that is of type `NoneType`? – Bryan Oakley Jul 19 '15 at 00:33

1 Answers1

2

There is a big difference between

user_input = Entry(root).pack(side=TOP)

AND

user_input = Entry(root)
user_input.pack(side=TOP)

Since pack function is returning None and not self. and that's why you should do this in two lines just like i wrote at the second block of code.

good luck.

omri_saadon
  • 10,193
  • 7
  • 33
  • 58