-2

(disclaimer: I'm not good at English, so nvm)

I recently started to learn a python GUI called tkinter. I'm not sure why but the get() function of Entry is not working.

My Code:

from tkinter import *
root = Tk()
root.geometry("500x300")

var = Entry(root, width = 50).pack()
text = var.get()

root.mainloop()

And here is the output:

Traceback (most recent call last):
File "D:\Python\Tkinter\Tkinter tutorial.py", line 8, in <module>
text = var.get()
AttributeError: 'NoneType' object has no attribute 'get'    <---- This this the problem
[Finished in 2.4s with exit code 1]
[cmd: ['py', '-u', 'D:\\Python\\Tkinter\\Tkinter tutorial.py']]
[dir: D:\Python\Tkinter]
[path: C:\Program Files\Python37;C:\Program Files\Python37\Scripts]
martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

0

A common mistake. All geometry managers return None. So, your variable var is None due to .pack(), as the result of last function is taken. Move the .pack() to the next line

var = Entry(root, width = 50)
var.pack()