Python 特金特和#x27;模块';对象没有属性';框架';
我用tkinter python编写了一个hello world应用程序,但我收到了下一条消息:“module”对象没有属性“Frame”Python 特金特和#x27;模块';对象没有属性';框架';,python,tkinter,tk,Python,Tkinter,Tk,我用tkinter python编写了一个hello world应用程序,但我收到了下一条消息:“module”对象没有属性“Frame” import _tkinter as tk 这里是错误 class Application(tk.Frame): def __init__(self, master=None): tk.Frame.__init__(self, master) self.pack() self.createWidget
import _tkinter as tk
这里是错误
class Application(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.pack()
self.createWidgets()
def createWidgets(self):
self.hi_there = tk.Button(self)
self.hi_there["text"] = "Hello World\n(click me)"
self.hi_there["command"] = self.say_hi
self.hi_there.pack(side="top")
self.QUIT = tk.Button(self, text="QUIT", fg="red",
command=root.destroy)
self.QUIT.pack(side="bottom")
def say_hi(self):
print("hi there, everyone!")
root = tk.Tk()
app = Application(master=root)
app.mainloop()
为什么会发生这种情况 您应该使用
Tkinter
(Tkinter
,如果您使用的是Python 3.x),而不是\u Tkinter
:
import Tkinter as tk
根据: 。。。Tk接口位于名为
\u tkinter
的二进制模块中。这
模块包含到Tk的低级接口,永远不应
应用程序程序员直接使用
不要将您的文件称为
tkinter.py
,如果需要,请重命名。这里的解决方案是对相关的Python版本使用正确的语法
Tkinter>>Python 2.x
tkinter>>Python 3.x
尽管如此,我还是有错误,因为我调用了我的文件tkinter.py,并且出现了错误:
module 'tkinter' has no attribute 'Frame'
一旦我将我的文件完全重命名为其他文件,在我的例子中,我选择了tk-testing.py,它在Python2.x和Python3.x中都很好,同时使用了上面正确的命名约定
python2.x
import Tkinter as tk
import Tkinter as tk
Python3.x
import Tkinter as tk
import Tkinter as tk
我很好奇。您是如何得出结论认为,
import\u tkinter as tk
是正确的?你在什么地方读过吗?这就是我的代码不起作用的原因。。。非常感谢…当然,如果目录中的任何文件(不仅仅是正在执行的文件)被命名为tkinter.py
,这个“bug”也会咬人。感谢您指出,一旦我将我的文件完全重命名为其他文件,文件本身就不应该是“tkinter.py”,在我的例子中,我选择了tk-testing.py,这很好,解决了我的问题