我用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.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()为什么会发生这种情况?
发布于 2014-01-08 22:00:04
应使用Tkinter (如果使用Python3.x,则为tkinter),而不是_tkinter
import Tkinter as tk根据Tkinter module documentation的说法
...Tk接口位于名为
_tkinter的二进制模块中。此模块包含Tk的低级接口,不应由应用程序程序员直接使用。
发布于 2017-05-10 06:22:07
不要将您的文件称为tkinter.py,如果需要,请重命名。
发布于 2017-10-19 16:43:39
这里的解决方案是为关联的Python版本使用正确的语法。
Tkinter >> Python2.x
tkinter >> Python3.x
尽管如此,我还是有错误,因为我已经将我的文件命名为tkinter.py,并且出现了错误:
module 'tkinter' has no attribute 'Frame'一旦我将我的文件重命名为完全不同的名称,在我的例子中,我选择了tk-testing.py,它在Python2.x和Python3.x中都很好,同时使用上面正确的命名约定。
Python 2.x
import Tkinter as tkPython 3.x
import Tkinter as tkhttps://stackoverflow.com/questions/20997761
复制相似问题