June 22nd, 2013, 03:36 PM
-
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.
June 23rd, 2013, 12:53 AM
-
June 23rd, 2013, 01:19 AM
-
The tkinter module is just not capitalized in python 3.
Do this: 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
June 23rd, 2013, 05:23 AM
-
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.
June 23rd, 2013, 07:17 AM
-
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.
June 23rd, 2013, 07:56 PM
-
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.)
June 24th, 2013, 05:34 AM
-
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.