ImportError: No module named Tkinter when importing swampy.TurtleWorld
ImportError: No module named Tkinter when importing swampy.TurtleWorld
我正在使用 Python 3.4 并遵循本书 "Think Python: how to think like a computer scientist"。我实际上在一周前就解决了这个问题,但是当它像上周那样无法 运行 时保存了原始代码。现在我有:
import tkinter
from swampy.TurtleWorld import *
产生:
ImportError: No module named 'Tkinter'
当我上周运行代码时,我大致记得在 'import tkinter' 行中,末尾有一部分看起来像这样:[Tkinter]
。我尝试了 import tkinter as Tkinter
但它不起作用。
如果我把它改成Python2.7。并且 运行
import Tkinter
from swampy.TurtleWorld import *
world = TurtleWorld()
bob = Turtle()
print (bob)
fd(bob, 100)
lt(bob)
fd(bob, 100)
wait_for_user()
TurtleWorld window 打开但没有海龟。我怎样才能让它再次工作(Python 3.4 首选)?
您正在尝试 运行 Python 2 Python 3 中的代码加载 Python 2 个特定模块 (Tkinter),但它无法正常工作。
默认的 TurtleWorld 软件包是 Python 2,但是 Green Tea Press 的 Swampy: Installation Instructions 页面中有一个不受支持的 Python 3 版本。转到底部的 Python 3 部分。您将需要手动安装此软件包,或者将其保存在您的工作目录中并从那里导入。 (说明对此进行了解释。)
另一种选择是使用 Python3 附带的 turtle 模块,因为它在功能上与大多数与 turtle 相关的实验相似。 (我已经使用 Python turtle 模块回答了 TurtleWorld 关于 SO 的问题。)例如:
from turtle import Turtle, Screen
bob = Turtle(shape="turtle")
print(bob)
bob.fd(100)
bob.lt(90)
bob.fd(100)
screen = Screen()
screen.exitonclick()
我正在使用 Python 3.4 并遵循本书 "Think Python: how to think like a computer scientist"。我实际上在一周前就解决了这个问题,但是当它像上周那样无法 运行 时保存了原始代码。现在我有:
import tkinter
from swampy.TurtleWorld import *
产生:
ImportError: No module named 'Tkinter'
当我上周运行代码时,我大致记得在 'import tkinter' 行中,末尾有一部分看起来像这样:[Tkinter]
。我尝试了 import tkinter as Tkinter
但它不起作用。
如果我把它改成Python2.7。并且 运行
import Tkinter
from swampy.TurtleWorld import *
world = TurtleWorld()
bob = Turtle()
print (bob)
fd(bob, 100)
lt(bob)
fd(bob, 100)
wait_for_user()
TurtleWorld window 打开但没有海龟。我怎样才能让它再次工作(Python 3.4 首选)?
您正在尝试 运行 Python 2 Python 3 中的代码加载 Python 2 个特定模块 (Tkinter),但它无法正常工作。
默认的 TurtleWorld 软件包是 Python 2,但是 Green Tea Press 的 Swampy: Installation Instructions 页面中有一个不受支持的 Python 3 版本。转到底部的 Python 3 部分。您将需要手动安装此软件包,或者将其保存在您的工作目录中并从那里导入。 (说明对此进行了解释。)
另一种选择是使用 Python3 附带的 turtle 模块,因为它在功能上与大多数与 turtle 相关的实验相似。 (我已经使用 Python turtle 模块回答了 TurtleWorld 关于 SO 的问题。)例如:
from turtle import Turtle, Screen
bob = Turtle(shape="turtle")
print(bob)
bob.fd(100)
bob.lt(90)
bob.fd(100)
screen = Screen()
screen.exitonclick()