Python 3 Tkinter: NameError with Entry widget: name 'Entry' is not defined

9.5k Views Asked by At

I am writing a GUI on Python 3 using Tkinter, but every time I use Entry(), I get a name error.

I tried a more simpler version of the code, (which is written below), but it still caused a NameError:

import tkinter
top = tkinter.Tk()

e = Entry(top)
e.pack()

top.mainloop()

This is the error I get:

Traceback (most recent call last):
  File "/home/pi/gui.py", line 4, in <module>
    e = Entry()
NameError: name 'Entry' is not defined

I only recently started coding again, so the answer is probably something extremely simple that I didn't realise was wrong with the code, but thanks for any answers.

3

There are 3 best solutions below

0
On BEST ANSWER

You didn't import it. Change your code to:

e = tkinter.Entry(top)

Or import it explicitly:

from tkinter import Entry
0
On

You didn't import Entry to the local namespace, so you'll need to access it from the module, which you did import:

e = tkinter.Entry(top)
2
On

Since you imported the tkinter module, every tkinter action needs to start with tkinter.[function name].

You could also just add:

from tkinter import [function name]

To import multiple functions you seperate them with a comma.

If you use a lot of functions, it is best to import every function, with

from tkinter import *