Why tkinter module raises attribute error when run via command line but not when run via IDLE?

前端 未结 2 1396
广开言路
广开言路 2020-12-18 13:07

Is there a reason why the code will raise an error when run via the command line compared to when run via IDLE\'s run module f5 command?

<
2条回答
  •  盖世英雄少女心
    2020-12-18 13:19

    Actually it's true that the module has no attribute filedialog, it's a submodule and you should import it as import tkinter.filedialog before using it. You can use tk.filedialog without explicitly importing filedialog in IDLE because it's already imported.

    import sys
    sys.modules['tkinter.filedialog']
    

    The above code will raise a KeyError in a standard python interpreter but it will return something like in IDLE.

提交回复
热议问题