1

导入整个包在 IDLE 中有效,但在 shell 中无效。以下在 IDLE 中工作正常:

import tkinter as tk
tk.filedialog.askopenfilename()

在 shell 中,我收到此错误:

AttributeError: 'module' object has no attribute 'filedialog'

我知道我必须import tkinter.filedialog在 shell 中完成这项工作。

为什么IDLE和shell有区别?我怎样才能让 IDLE 像 shell 一样?让脚本在 IDLE 中运行而在 shell 中失败可能会令人沮丧。

我正在使用 Python 3.4。

4

1 回答 1

1

这是我为未来的 3.5.3 和 3.6.0a4 版本修复的 IDLE 错误。 追踪器问题。

对于现有的 3.5 或 3.4 版本,请将以下内容添加到 idlelib/run.py 中,就在 LOCALHOST 行之前。

for mod in ('simpledialog', 'messagebox', 'font',
            'dialog', 'filedialog', 'commondialog',
            'colorchooser'):
    delattr(tkinter, mod)
    del sys.modules['tkinter.' + mod]

我认为这将适用于早期的 3.x 版本,但没有安装它们进行测试。对于现有的 3.6.0a_ 版本,将“colorchooser”替换为“ttk”。

于 2016-08-06T20:28:49.960 回答