Reputation: 391
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
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
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
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