Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
490 views
in Technique[技术] by (71.8m points)

python - Script fails with AttributeError when run in CMD, but executes fine in IDLE

I am quite new and confused. I tried a simple script with tkinter and it worked fine in IDLE but when i try to launch it from CMD - the tkinter window opens and it looks fine , but when you attempt to click any button or file menu options an AttributeError raises:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:Python33libkinter\__init__.py", line 1489, in __call__
return self.func(*args)
  File "060214_Manual_Evaluation_of_Protein-protein_Cross-Links.py", line 13, in Open_csv
  self.CsvPath = tk.filedialog.askopenfilename()
  AttributeError: 'module' object has no attribute 'filedialog'

I am thankful for any input or where i could possibly find more information about differences between IDLE and CMD.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

People asked about python version because tk.filedialog is spelled differently in 2.x. However, I suspect that your problem is that Idle runs code in a managed environment that masks a bug in your unposted code of not properly importing tkinter.filedialog. To illustrate, the follow is from the standard 3.4.2 console interpreter

>>> import tkinter as tk
>>> tk.filedialog
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'filedialog'

Here are the same statements in Idle's Shell.

>>> import tkinter as tk
>>> tk.filedialog
<module 'tkinter.filedialog' from 'C:\Programs\Python34\lib\tkinter\filedialog.py'>

The reason that there is no error is because Idle has already imported the filedialog submodule as tkinter.filedialog (in sys.modules). If this is your problem also, a solution for you is to add the import below and refer to 'filedialog' without the 'tk' prefix.

>>> from tkinter import filedialog
>>> filedialog
<module 'tkinter.filedialog' from 'C:\Programs\Python34\lib\tkinter\filedia
log.py'>
>>> filedialog.askopenfilename
<function askopenfilename at 0x0000000000498BF8>

If this does not solve this issue, edit your question to add a truly minimal code example and explain exactly how you run both with Idle and 'CMD' (is this cmd.exe on Windows, or what?).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...