这是我第一次在python中使用turtle模块,但我似乎无法导入它? 这是我的代码:
import turtle
turtle.shape("turtle")
turtle.speed(1)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.exitonclick()
我将其运行为 $ python3 example.py
我明白了
$ python3 example.py
Traceback (most recent call last):
File "example.py", line 1, in <module>
from turtle import *
File "/usr/lib/python3.7/turtle.py", line 107, in <module>
import tkinter as TK
ModuleNotFoundError: No module named 'tkinter'
如果我运行Python2 python example.py
$ python example.py
Traceback (most recent call last):
File "example.py", line 1, in <module>
import turtle
File "/usr/lib/python2.7/lib-tk/turtle.py", line 107, in <module>
import Tkinter as TK
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 42, in <module>
raise ImportError, str(msg) + ', please install the python-tk package'
ImportError: No module named _tkinter, please install the python-tk package
最佳答案
这是一个很容易解决的问题:
sudo apt-get install python3-tk
其他评论者之一建议使用 python-tk,但它在这里不适合您。
关于Python 模块 turtle 未正确导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61502272/