1. 程式人生 > >python提示NameError: name 'xxx' is not defined

python提示NameError: name 'xxx' is not defined

在使用Tkinter時,使用python自己的IDLE執行程式沒問題,當使用命令提示符模式執行會出現錯誤NameError: name 'Tk' is not defined。錯誤出現在
from tkinter import *
top = Tk()

問題的原因在於程式檔名為tkinter.py,當使用命令提示符執行時,工作路徑切換到了tkinter.py所在路徑,匯入模組會首先查詢當前路徑。

將檔名改為其他,如tkinter_test.py,結果正確。