2016-11-05 106 views
-2

我想使用Tkinter在文本“t”中创建一个基本窗口,但是当运行代码时shell会吐出“NameError:name'标签'未定义”。我正在运行Python 3.5.2。在tkinter应用程序中未定义的标签

我跟着教程,但问题是在label = Label(root, text="test")行。

import tkinter 

root = tkinter.Tk() 
sheight = root.winfo_screenheight() 
swidth = root.winfo_screenwidth() 
root.minsize(width=swidth, height=sheight) 
root.maxsize(width=swidth, height=sheight) 

label = Label(root, text="test") 
label1.pack() 

root = mainloop() 

3.5.2中的标签功能是否不同?

+2

确实意味着'tkinter.Label'由于你怎么导入呢? '标签'是*类*,不是功能。 – Li357

回答

1

您从未导入Label类。尝试tkinter.Label

检查这些教程

import语句也许他们暗示from tkinter import *

0
import tkinter 

root = tkinter.Tk() 
sheight = root.winfo_screenheight() 
swidth = root.winfo_screenwidth() 
root.minsize(width=swidth, height=sheight) 
root.maxsize(width=swidth, height=sheight) 

label = tkinter.Label(root, text="test") 
label1.pack() 

root = tkinter.mainloop() # <- prob need to fix this as well. 

因为你没有做你from tkinter import *需要从Tkinter的模块调用的标签。

或者你可以这样做:

from tkinter import * 
... 
label = Label(root, text="test") 
相关问题