简体   繁体   English

Python 3 Tkinter:带有Entry小部件的NameError:未定义名称'Entry'

[英]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. 我正在使用Tkinter在Python 3上编写GUI,但是每次使用Entry()时,都会出现名称错误。

I tried a more simpler version of the code, (which is written below), but it still caused a NameError: 我尝试了更简单的代码版本(如下所述),但它仍然引起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. 我只是最近才再次开始编码,因此答案可能是一个非常简单的问题,我没有意识到代码有问题,但是感谢您的回答。

You didn't import it. 您没有导入它。 Change your code to: 将您的代码更改为:

e = tkinter.Entry(top)

Or import it explicitly: 或显式导入:

from tkinter import Entry

您没有将Entry导入到本地名称空间,因此您需要从导入的模块中访问它:

e = tkinter.Entry(top)

Since you imported the tkinter module, every tkinter action needs to start with tkinter.[function name]. 由于您导入了tkinter模块,因此每个tkinter操作都必须以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 *

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM