Thread: Tkinter issue

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    22
    Rep Power
    0

    Tkinter issue


    Hi, I downloaded a package associated with a free online textbook 'thinkpython', the text is written for python 2 but states the example should also work in python 3. The issue I have is that when I try and import from the package it gives me the error 'Importerror: no module named Tkinter'. I am using python 3 idle shell and tkinter can be imported and tests ok. it seems the package is written for use with Tkinter and not tkinter and I cant seem to import the old version of tkinter in python 3. I havent explained this well lol, but some help with this issue would be much appreciated. cheers.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    22
    Rep Power
    0
    any ideas?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    3
    The tkinter module is just not capitalized in python 3.
    Do this:
    Code:
    import tkinter
    Or if you want to remain compatible with both py2 and 3:
    Code:
    try:
        import tkinter as tk
    except ImportError:
        import Tkinter as tk
    -Mek
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    22
    Rep Power
    0
    This is the code and what it produces

    import tkinter
    tkinter._test()

    the little test window opens, so I know it works, then -

    from swampy.TurtleWorld import*

    and I get this-

    Traceback (most recent call last):
    File "<pyshell#10>", line 1, in <module>
    from swampy.TurtleWorld import*
    File "C:\Python33\lib\site-packages\swampy\TurtleWorld.py", line 8, in <module>
    from Tkinter import TOP, BOTTOM, LEFT, RIGHT, END, LAST, NONE, SUNKEN
    ImportError: No module named 'Tkinter'

    I assumed python 3 was backwards compatible.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    22
    Rep Power
    0
    I decided to just install python 2.7.5 and run all the examples through that. Also had to download pychoose to alternate between different versions at the command line. Its annoying.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    The problem here is that swampy.TurtleWorld, whatever that is, is importing Tkinter under the old name. If you're willing to use a little bit of black magic you can do something like this:
    Code:
    import sys
    import tkinter
    sys.modules["Tkinter"] = tkinter
    from swampy.TurtleWorld import *
    This should allow it to work on Python 3, provided that the only incompatibility in that module is that different spelling of Tkinter. (Admittedly, there's a good chance it's not.)
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    22
    Rep Power
    0
    Nyktos-Thanks for the reply. Ive got it running in python 2.7.5 now and it imports fine. Im probably not going to try the tkinter conversion as it took me hours to figure out how to alternate between different versions of python at the command line, so if your code threw up any more bugs it would probably take me days to work out lol. Im new to python/programming and ive learnt a lot just trying to sort out how to download packages and sort the PATH out.

IMN logo majestic logo threadwatch logo seochat tools logo