编程语言
首页 > 编程语言> > python-在CMD中运行时,脚本失败并出现AttributeError,但在IDLE中执行正常

python-在CMD中运行时,脚本失败并出现AttributeError,但在IDLE中执行正常

作者:互联网

我很新,很困惑.我用tkinter尝试了一个简单的脚本,它在IDLE中工作正常,但是当我尝试从CMD启动它时-tkinter窗口打开,看起来还不错,但是当您尝试单击任何按钮或文件菜单选项时,就会引发AttributeError:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python33\lib\tkinter\__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'

我很感谢您的任何投入,或者在这里我可以找到有关IDLE和CMD之间差异的更多信息.

解决方法:

人们问起python版本是因为tk.filedialog在2.x中拼写不同.但是,我怀疑您的问题是空闲在托管环境中运行代码,该环境掩盖了未正确发布tkinter.filedialog的未发布代码中的错误.为说明起见,以下内容来自标准的3.4.2控制台解释器

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

这是Idle的Shell中的相同语句.

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

没有错误的原因是,空闲已将filedialog子模块导入为tkinter.filedialog(在sys.modules中).如果这也是您的问题,那么为您提供的解决方案是在下面添加导入,并引用不带’tk’前缀的’filedialog’.

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

如果这不能解决此问题,请编辑您的问题以添加一个真正的最小代码示例,并确切说明如何同时使用Idle和’CMD'(在Windows上是cmd.exe还是什么?).

标签:python-idle,cmd,python
来源: https://codeday.me/bug/20191028/1954340.html