为什么我不能在VS-Python中导入turtle或turtle.Screen?

时间:2019-05-25 17:28:34

标签: python turtle-graphics

我已经坚持了很长时间没有得到回应。我已经尝试过执行import turtle, wn = turtle.Screen()之类的命令,但是这些命令都不适合我的Visual Studio。

一旦我键入这些命令,它在“问题”或输出栏中就什么也没有说,但是什么也没发生。就像没有乌龟屏幕弹出一样。

2 个答案:

答案 0 :(得分:0)

这是一个有效的乌龟程序。 测试您的IDE是否可以使用它-输入50、4和60,您将获得以下输出:

demo output

import math
import turtle

def squareFromCurrPosAndRotationAsCenter(bob, s):
    """Starting from current position and rotation, draw a square of sidelength s.
    End on same position and rotation you began with."""
    # goto first side
    bob.penup()
    bob.forward(s/2)
    bob.pendown()
    # draw a half side
    bob.right(90)
    bob.forward(s/2)
    # draw three full sides
    for k in range(3):
        bob.right(90)
        bob.forward(s)
    # draw last half side
    bob.right(90)
    bob.forward(s/2)

    # goto back to origin
    bob.penup()
    bob.right(90)
    bob.forward(s/2)
    # turn back in original direction
    bob.right(180) 


def getInt(text, default): 
    """Try to parse input to int, return default if not possible"""
    try:
        return int(input(text))
    except:
        return default

def symetricSquares():
    # Get user input or (when error) use default
    size = getInt('Enter size for top square: ', 50)
    num_squares = getInt('Enter the amount of squares: ', 4)
    angle = getInt('Enter increase of starting angle: ', 60)

    # Create a turtle
    bob = turtle.Turtle()

    bob.speed(max(5,(num_squares * 360/angle)//10)) 

    # outer loop changes starting angle
    for startAngle in range(0,360-angle+1,angle):
        bob.setheading(startAngle)

        # we use a list comp to create the desired square sizes
        # you could also do [size, size*2, size*3, size*4] if 
        # you want always 4 circles

        for s in [size*(n+1) for n in range(num_squares)]:
            squareFromCurrPosAndRotationAsCenter(bob, s)

    turtle.mainloop()


def main():
    symetricSquares()

main()

如果这不起作用,则必须查看是否缺少包裹。

答案 1 :(得分:0)

样本最少,效果很好

''' sample.py '''
import turtle

s = turtle.Screen()
t = turtle.Turtle()
t.forward(100)
s.exitonclick()

运行后,您将获得

> python3 ./sample.py

enter image description here