2014-01-08 50 views
2

我寫的Tkinter蟒蛇一個Hello World應用程序,但我recive下一條信息:「模塊」對象有沒有屬性「框架」Tkinter的,「模塊」對象有沒有屬性「框架」

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() 

爲什麼會發生這種情況?

+0

我很好奇。你是如何得出結論的:'將tt進口爲tk'是正確的?你有沒有讀過它? –

回答

3

您應該使用Tkintertkinter如果你使用Python 3.X),不_tkinter

import Tkinter as tk 

根據Tkinter module documentation

... The Tk interface is located in a binary module named _tkinter . This module contains the low-level interface to Tk, and should never be used directly by application programmers.

1

不要叫你的文件tkinter.py,必要時重命名。

0

這裏的解決方案是爲關聯的Python版本使用正確的語法。

的Tkinter的Python >> 2.x的

的Tkinter >> Python 3.x都有

儘管這樣,我有錯誤,因爲我已經打電話給我的文件tkinter.py,並且是帶有錯誤:

module 'tkinter' has no attribute 'Frame' 

有一次,我改名爲我的文件完全是另一回事,在我的情況下,我選擇了tk-testing.py在Python 2.x和Python 3.x中都很好,同時使用了上述正確的命名約定。

的Python 2.x的

import Tkinter as tk 

Python 3.x都有

import Tkinter as tk 
相關問題