0

I am writing a function that takes a textbox object from the tkinter library as an argument. When I fill in the textbox and hit the button, I get

"AttributeError: 'NoneType' object has no attribute 'get'." 

I know for a fact the textbox object has get() as a function. I even imported the tkinter library into the file that has my function. Here's a simplified version of what I am trying to do in two files:

main:

import tkinter
import save_file
app = tkinter.Tk()
textbox = tkinter.Text(app).pack()
button = tkinter.Button(app, command=lambda: save_file.save_file(textbox))

save_file:

import tkinter
def save_file(textbox):
    text = textbox.get()

Can anyone tell me what I am doing wrong?

user3291465
  • 23
  • 1
  • 1
  • 3
  • I'm not sure that's the closest dup for this question, but you can find plenty of others in the Linked and Related lists from there. – abarnert Oct 24 '14 at 22:16

3 Answers3

3

pack() returns None; you want to store just the Text() object, then call pack() on it separately:

textbox = texinter.Text(app)
textbox.pack()
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Yep that's all it took. Awesome! Thanks! To clarify though, the problem was that my choice of syntax was actually assigning the value returned by pack() to my variable? – user3291465 Oct 25 '14 at 16:49
  • Exactly; you chained the calls and the return value of `texinter.Text()` was used to look up the `pack` method, and the expression ends up producing what that last method call produces, not the intermediary results. – Martijn Pieters Oct 25 '14 at 16:52
0

tkinter.Text(app).pack() returns None so you set textbox equal to None

Change to:

 textbox = tkinter.Text(app)
 textbox.pack()
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
0

Your problem is that the .pack() method on a tkinter Text object returns None.

The fix:

import tkinter
import save_file
app = tkinter.Tk()
textbox = tkinter.Text(app)
textbox.pack()
button = tkinter.Button(app, command=lambda: save_file.save_file(textbox))
BingsF
  • 1,269
  • 10
  • 15