代码之家  ›  专栏  ›  技术社区  ›  mcu

从IDLE与Shell中的包导入

  •  1
  • mcu  · 技术社区  · 10 年前

    import tkinter as tk
    tk.filedialog.askopenfilename()
    

    在shell中,我得到以下错误:

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

    我知道我必须 import tkinter.filedialog 要在shell中工作。

    为什么IDLE和shell有区别?我怎样才能让IDLE表现得像壳一样?脚本在IDLE中工作,而在shell中失败,这可能会令人沮丧。

    我使用的是Python 3.4。

    1 回复  |  直到 10 年前
        1
  •  1
  •   Terry Jan Reedy    10 年前

    这是一个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”。

    推荐文章