0

I have tried to work with the Tkinter library, however, I keep getting this message, and I don't know how to solve it.. I looked over the net but found nothing to this specific error - I call the library like this:

from Tkinter import *

and I get this error -

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

I have no clue what can I do now.. Thank you

full traceback:

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'
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
ErezProductions
  • 41
  • 3
  • 11

4 Answers4

2

You imported (mostly) everything from the module with from Tkinter import *. That means that (mostly) everything in that module is now included in the global namespace, and you no longer have to include the module name when you refer to things from it. Thus, refer to Tkinter's TclError object as simply TclError instead of Tkinter.TclError.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
2

The problem seems to be in "C:\Heights\PortableApps\PortablePython2.7.6.1\App\lib\lib-tk\Tkinter.py:

The regular python install imports in lib-tk\Tkinter.py are different to what is in 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

Then where Tkinter is used in PortablePython _tkinter is used instead. It seems like a bug in PortablePython.

The full contents of the file are here. Replacing the file in C:\Heights\PortableApps\PortablePython2.7.6.1\App\lib\lib-tk\Tkinter.py as per the comments fixes the issue.

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
1

Like @ErezProductions said. You either have to import everything and access it directly or import only the module.

from Tkinter import *
TclError

or

import Tkinter
Tkinter.TclError
Saimu
  • 1,254
  • 2
  • 9
  • 11
0

See the difference:

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

No error. But, with your method:

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

The difference is that the first method imports the module tkinter into the name space. You can address its properties using the dot notation tinter.property. However, the from tkinter import * imports the module's properties into the name space, not the module itself.

Either try the first method given above, or adjust your approach (NB: importing all properties is a bad idea) like so:

>>> from tkinter import *
>>> my_TclError = TclError   # renamed because TclError defined in tkinter
>>>
dsclose
  • 556
  • 5
  • 15