Bill Reason
Bill Reason

Reputation: 391

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

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.

Upvotes: 2

Views: 9849

Answers (3)

Pokestar Fan
Pokestar Fan

Reputation: 174

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 *

Upvotes: 2

Danil Speransky
Danil Speransky

Reputation: 30453

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

e = tkinter.Entry(top)

Or import it explicitly:

from tkinter import Entry

Upvotes: 5

Zach Gates
Zach Gates

Reputation: 4130

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)

Upvotes: 3

Related Questions