2016-08-30 93 views
-1

我的代码给了我这个错误,我不能为我的生活弄清楚为什么它告诉我“NameError:name'Button'没有被定义。”在Tkinter中,我认为Button应该添加一个按钮?为什么我的代码返回“名称”按钮“未定义”?

import Tkinter 

gameConsole = Tkinter.Tk() 
#code to add widgets will go below 

#creates the "number 1" Button 
b1 = Button(win,text="One") 

gameConsole.wm_title("Console") 
gameConsole.mainloop() 
+0

按钮是'TKinter'命名空间的一部分。使用'TKinter.Button(...)'。请记住,对于新的代码库,推荐的解释器版本是Python 3. Python 2支持在2020年结束。 –

回答

1

提供给源命名空间有几个选项:

  • from Tkinter import Button导入特定的类。

  • import Tkinter - >b1 = Tkinter.Button(win,text="One")指定内联命名空间。

  • from Tkinter import *从模块导入所有内容。

+0

感谢您的解决方案! –

+0

@AveryLipsit记得提出答案和/或选择它,如果它解决了你的问题。 –

0

使用

import Tkinter 
b1 = Tkinter.Button(win,text="One") 

from Tkinter import Button 
b1 = Button(win,text="One") 
+0

谢谢,我现在也明白为什么它这么做了! –

相关问题