在 VS Code / Mac M1 上运行海龟图形

问题描述 投票:0回答:1

这是我尝试运行的代码:

#Draw equilateral triangle
from turtle import *
forward(60)
left(120)
forward(60)
left(120)
forward(60)

使用 Idle 工作正常,但当我尝试使用 VS Code 时,我得到:

Traceback (most recent call last):
  File "/Users/padeasey/Documents/OpenUniversity/TM112/Block1/python_scripts/draw_equilateral_triangle.py", line 2, in <module>
    from turtle import *
  File "/opt/homebrew/Cellar/[email protected]/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib/python3.10/turtle.py", line 107, in <module>
    import tkinter as TK
  File "/opt/homebrew/Cellar/[email protected]/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib/python3.10/tkinter/__init__.py", line 37, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'

我尝试了

pip3 install tk
,它现在出现在 pip list tk 0.1.0 中,但这没有帮助。 它也适用于空闲。

python visual-studio-code turtle-graphics
1个回答
0
投票

turtle 实际上并不是一个纯 python 模块。它实际上只是 tkinter 模块的绘图工具。所以使用

pip install tkinter
应该可以解决这个问题。

但是。有更好的解决方案。如果你真的想解决这个问题。那么最好的方法是安装 python 并启用所有功能。尝试打开您使用的 python 安装程序(用于安装 python 的应用程序),然后选择您可以选择的所有内容。如果他们向您提供某些内容,请选择它(除非它被禁用,这意味着您需要执行其他操作才能激活它,在这种情况下,请忽略它)。这样,您不仅可以获得 tkinter 和turtle,还可以获得大多数 STD 实用程序,例如服务器或其他类型或您可能需要的任何其他东西。

© www.soinside.com 2019 - 2024. All rights reserved.