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

发布于 2021-01-29 15:05:20

我很新,很困惑。我用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之间差异的更多信息。

关注者
0
被浏览
99
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    人们问起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’s 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还是什么?)。



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看