ImportError: No module named Tkinter when importing swampy.TurtleWorld

百般思念 提交于 2019-12-02 16:02:32

问题


I am using Python 3.4 and following along the book "Think Python: how to think like a computer scientist". I actually figured out this issue a week ago, but saved over the original code when it failed to run like it did last week. Right now I have:

import tkinter
from swampy.TurtleWorld import *

which yields:

ImportError: No module named 'Tkinter'

When I had the code working last week, I loosely recall that in the 'import tkinter' line, there was a portion at the end that looked like this: [Tkinter]. I tried import tkinter as Tkinter but it doesn't work.

If I change it to Python2.7. And run

import Tkinter
from swampy.TurtleWorld import *
world = TurtleWorld()
bob = Turtle()
print (bob)
fd(bob, 100)
lt(bob)
fd(bob, 100)
wait_for_user()

The TurtleWorld window opens but there is no turtle. How can I get this to work again (Python 3.4 preferred)?


回答1:


You're trying to run Python 2 code in Python 3 that loads Python 2 specific modules (Tkinter) and it's not going to work.

The default TurtleWorld package is Python 2, but there is an unsupported Python 3 version from the Green Tea Press' Swampy: Installation Instructions page. Go to the Python 3 section at the bottom. You will either need to install this package manually or just keep it in your working directory and import it from there. (The instructions explain this.)

Another alternative is to use the turtle module that comes with Python 3, as it's functionally similar for most turtle-related experiments. (I've answered TurtleWorld questions on SO using the Python turtle module.) For example:

from turtle import Turtle, Screen

bob = Turtle(shape="turtle")
print(bob)
bob.fd(100)
bob.lt(90)
bob.fd(100)

screen = Screen()
screen.exitonclick()


来源:https://stackoverflow.com/questions/40964854/importerror-no-module-named-tkinter-when-importing-swampy-turtleworld

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!