2014-01-12 213 views
0

我想學習python tkinter,但tkinter中的許多名稱(根據文檔)給我錯誤。我試圖與PyDev的Eclipse和即使編輯告訴我「tkinter.font」是好的它給了我一個錯誤,當我運行此:Python名稱錯誤

進口的Tkinter

打印(tkinter.font)

AttributeError:'模塊'對象沒有屬性'字體'

如果我在IDLE中試用它,但我在IDLE中嘗試了其他一些名稱,例如scrolledtext,turtle,dnd,它們給我屬性錯誤。

有人可以幫忙嗎?這真讓我抓狂。

回答

2

tkinter.font是在Tkinter包中的模塊。這意味着,通常,當您執行import tkinter時,它不會被導入。

要訪問tkinter.font,你必須明確地導入:

>>> import tkinter 
>>> tkinter.font 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
AttributeError: 'module' object has no attribute 'font' 
>>> 
>>> import tkinter.font 
>>> tkinter.font 
<module 'tkinter.font' from 'C:\\Python33\\lib\\tkinter\\font.py'> 
>>> 

然而對於某些原因,tkinter.font進口的,當你在IDLE做import tkinter。我不知道這個原因,但我猜想這是爲了方便。無論如何,最好總是明確導入tkinter.font(以及類似的模塊),這樣你的代碼就可以在IDLE內部和外部工作。

+0

ohhh,由於某些原因,我忘記了模塊沒有導入。我在IDLE中完成了dir(tkinter)並看到了字體,所以我想我可以使用它。如果IDLE沒有讓我更困惑,我可能會意識到我的錯誤。我認爲我的安裝有問題。謝謝。 – user3162693