为什么tkinter模块在通过命令行运行时引发属性错误但在通过IDLE运行时不会引发属性错误? [英] Why tkinter module raises attribute error when run via command line but not when run via IDLE?

查看:268
本文介绍了为什么tkinter模块在通过命令行运行时引发属性错误但在通过IDLE运行时不会引发属性错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与通过IDLE的运行模块f5 命令运行相比,通过命令行运行时代码是否会引发错误的原因是什么?

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?

最近我一直在努力提高代码的可读性和健壮性。因此,我一直在尝试从模块导入* 行中删除所有。我以前在tkinter import * 中使用,我的代码行完全正常:

Recently I've been trying to improve the readability and robust-ness of my code. As a result I've been trying to remove all the from module import * lines. I used to use from tkinter import * and this line of my code worked perfectly fine:

self.path = filedialog.askdirectory()

但是现在我已经从tkinter import * code>到将tkinter导入为tk ,我已相应更改了代码:

But now I have changed from tkinter import * to import tkinter as tk and I have changed the code accordingly:

self.path = tk.filedialog.askdirectory()

名为GUI.py的文件导入此文件:来自lib.filesearch import * (我提到的代码行位于filesearch文件中。)

A file called GUI.py imports this file with: from lib.filesearch import * (the line of code I mentioned resides within the filesearch file.)

我通过IDLE运行我的代码一切都很好。我的GUI仍然有效,行 self.path = tk.filedialog.askdirectory()的工作方式与正常情况相同,但是当我通过Windows命令行运行代码时,我收到错误:

I run my code via IDLE and everything is fine. My GUI still works and the line self.path = tk.filedialog.askdirectory() works like normal however, when I run the code through windows command line I get the error:

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

以下是我的代码中的相关位:

Here are the relevant bits from my code:

来自filesearch.py​​

From filesearch.py

import tkinter as tk
    def get_path(self):
        """Store user chosen path to search"""
        self.paths = tk.filedialog.askdirectory(initialdir = FileSearch.DEFAULT)
        return self.paths

来自GUI.py

from lib.filesearch import *    
    def Browse(self):
        self.BrowseB['state']='disabled'
        self.p=self.CrawlObj.get_path()
        self.AddText('Searching from Path: ' + str(self.p))
        self.BrowseB['state']='normal'

与此问题我只安装了一个版本的python。即,Python34。

Unlike this question I only have one version of python installed. Namely, Python34.

推荐答案

由于 tkinter 的结构,你必须明确导入要加载的子模块:

Because of the structure of tkinter you must explicitly import submodules for them to load:

import tkinter as tk
print(hasattr(tk,"filedialog"))
import tkinter.filedialog
print(hasattr(tk,"filedialog"))

你不需要在IDLE中执行此操作的原因是,在运行代码之前,IDLE会在后台设置一些内容并最终导入一些tkinter库。其中一位维护者已评论这实际上是IDLE中的一个错误。

the reason you don't need to do this in IDLE is that before your code is run IDLE sets up some stuff in the background and ends up importing some of the tkinter libraries. One of the maintainers has commented that this is effectively a bug in IDLE.

实际上有一个在IDLE中执行代码时幕后发生的事情数量:

There are in fact a number of things happening behind the scenes when you execute code in IDLE:

>>> import inspect
>>> my_frame = inspect.currentframe()
>>> prev_frame = my_frame.f_back
>>> prev_frame.f_globals["__file__"]
'/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/idlelib/run.py'
>>> prev_frame.f_locals
{'self': <idlelib.run.Executive object at 0x10525b8d0>, 'code': <code object <module> at 0x1056874b0, file "<pyshell#1>", line 1>}
>>> import sys
>>> sys.stdout
<idlelib.PyShell.PseudoOutputFile object at 0x1053089e8>

控制程序的子进程已经导入了许多空闲的子模块,其中很多都是 import tkinter 因为空闲本身是用tkinter构建的。如果你在一个全新的python解释器上运行上面相同的代码:

The subprocess controlling your program has already imported many sub modules of idle and many of them import tkinter since idle itself is built with tkinter. if you run the same code above on a fresh new python interpreter:

>>> import inspect
>>> my_frame = inspect.currentframe()
>>> prev_frame = my_frame.f_back
>>> print(prev_frame)
None

IDLE的设计使这些差异很小,但是不能让它们完全透明。

IDLE has been designed so that these discrepancies are minimal, but can't make them completely transparent.

这篇关于为什么tkinter模块在通过命令行运行时引发属性错误但在通过IDLE运行时不会引发属性错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆