【问题标题】:Python Tkinter ErrorPython Tkinter 错误
【发布时间】:2015-11-23 12:11:18
【问题描述】:

我尝试使用 Tkinter 库,但是,我不断收到此消息,我不知道如何解决它。我查看了网络,但没有发现这个特定错误 - 我调用了库像这样:

from Tkinter import *

我得到了这个错误 -

    TclError = Tkinter.TclError
    AttributeError: 'module' object has no attribute 'TclError'

我不知道我现在能做什么.. 谢谢

完整的追溯:

Traceback (most recent call last):
File "C:/Users/Shoham/Desktop/MathSolvingProject/Solver.py", line 3, in <module>
from Tkinter import *
File "C:\Heights\PortableApps\PortablePython2.7.6.1\App\lib\lib-    tk\Tkinter.py", line 41, in <module>
TclError = Tkinter.TclError
AttributeError: 'module' object has no attribute 'TclError'

【问题讨论】:

  • 你在使用 PortablePython 吗?
  • @PadraicCunningham 是的,我愿意
  • 添加指向 "C:\Heights\PortableApps\PortablePython2.7.6.1\App\lib\lib- tk\Tkinter.py 中内容的链接,我觉得它看起来不对
  • @PadraicCunningham “添加链接”是什么意思?
  • 复制文件内容到pastebin

标签: python user-interface syntax tkinter portable-python


【解决方案1】:

您使用from Tkinter import * 从模块中导入(大部分)所有内容。这意味着(大部分)该模块中的所有内容现在都包含在全局命名空间中,并且当您从中引用内容时不再需要包含模块名称。因此,将TkinterTclError 对象简称为TclError 而不是Tkinter.TclError

【讨论】:

  • 我知道,但是,我在我的代码中甚至没有提到它。我刚刚创建了一些 GUI,我得到了这个错误
  • 也许您应该在问题中包含完整的回溯。
  • 我做到了。你可以看看
【解决方案2】:

看看区别:

>>> import tkinter
>>> TclError = tkinter.TclError
>>>

没有错误。但是,用你的方法:

>>> from tkinter import *
>>> TclError = tkinter.TclError
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'tkinter' is not defined

不同之处在于第一种方法将模块tkinter 导入到名称空间中。您可以使用点符号tinter.property 来处理其属性。但是,from tkinter import * 将模块的 属性 导入名称空间,而不是模块本身。

要么尝试上面给出的第一种方法,要么像这样调整你的方法(NB:importing all properties is a bad idea):

>>> from tkinter import *
>>> my_TclError = TclError   # renamed because TclError defined in tkinter
>>>

【讨论】:

  • 不,我仍然得到我在问题中给出的回溯(再看看我添加了更多细节)
【解决方案3】:

就像@ErezProductions 所说。您要么必须导入所有内容并直接访问它,要么只导入模块。

from Tkinter import *
TclError

import Tkinter
Tkinter.TclError

【讨论】:

  • 不,我仍然得到我在问题中给出的回溯(再看看我添加了更多细节)
【解决方案4】:

问题似乎出在"C:\Heights\PortableApps\PortablePython2.7.6.1\App\lib\lib-tk\Tkinter.py:

lib-tk\Tkinter.py 中的常规 python 安装导入与 PortablePython2.7.6.1\App\lib\lib-tk\Tkinter.py 中的不同:

try:
    import _tkinter
except ImportError, msg:
    raise ImportError, str(msg) + ', please install the python-tk package'
tkinter = _tkinter # b/w compat for export
TclError = _tkinter.TclError

然后在 PortablePython 中使用 Tkinter 的地方使用 _tkinter 代替。这似乎是PortablePython 中的一个错误。

文件的完整内容是here。根据 cmets 替换 C:\Heights\PortableApps\PortablePython2.7.6.1\App\lib\lib-tk\Tkinter.py 中的文件可以解决问题。

【讨论】:

    猜你喜欢
    • 2021-07-24
    • 2013-06-22
    • 2022-01-01
    • 2015-11-20
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多