从 IDLE 中的包导入 vs Shell
Importing from a Package in IDLE vs Shell
导入整个包在 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.
这是一个 IDLE 错误,我为未来的 3.5.3 和 3.6.0a4 版本修复了它。 Tracker issue.
对于现有的 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'。
导入整个包在 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.
这是一个 IDLE 错误,我为未来的 3.5.3 和 3.6.0a4 版本修复了它。 Tracker issue.
对于现有的 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'。