当在CMD中运行时,脚本失败并带有AttributeError,但在IDLE中执行得很好 [英] Script fails with AttributeError when run in CMD, but executes fine in IDLE

查看:1117
本文介绍了当在CMD中运行时,脚本失败并带有AttributeError,但在IDLE中执行得很好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新,很困惑。我尝试了一个简单的脚本与tkinter,它在IDLE工作正常,但当我尝试从CMD启动它 - tkinter窗口打开,它看起来不错,但当你试图单击任何按钮或文件菜单选项一个AttributeError引发: p>

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:\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之间差异的更多信息。

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

推荐答案

人们问关于python版本,因为tk.filedialog在2.x中拼写不同。但是,我怀疑你的问题是Idle运行代码在托管环境中,掩盖了未正确导入tkinter.filedialog的未发布代码中的错误。为了说明,下面是标准的3.4.2控制台解释器

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'

in Idle's Shell。

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'>

没有错误的原因是因为Idle已经将filedialog子模块导入为 tkinter.filedialog (在sys.modules中)。如果这也是你的问题,你的解决方案是添加下面的导入,并参考没有'tk'前缀的filedialog。

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>

如果这不能解决这个问题,请编辑您的问题,添加一个真正最小的代码示例,如何运行与Idle和'CMD'(这是cmd.exe在Windows上,或什么?)。

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?).

这篇关于当在CMD中运行时,脚本失败并带有AttributeError,但在IDLE中执行得很好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆