【问题标题】:Why cant i import turtle nor turtle.Screen in VS - Python?为什么我不能在 VS - Python 中导入 turtle 而不是 turtle.Screen?
【发布时间】:2019-05-25 17:28:34
【问题描述】:

我一直坚持这个问题很久没有得到回应。我已经尝试过执行import turtle, wn = turtle.Screen() 之类的命令,但这些命令都不适用于我的 Visual Studio。

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

【问题讨论】:

    标签: python turtle-graphics


    【解决方案1】:

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

    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()
    

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

    【讨论】:

      【解决方案2】:

      最少的样本,效果很好

      ''' sample.py '''
      import turtle
      
      s = turtle.Screen()
      t = turtle.Turtle()
      t.forward(100)
      s.exitonclick()
      

      运行后会得到

      > python3 ./sample.py
      

      【讨论】:

      • Oo.oO 事情是当我输入这些命令时它不起作用,就像什么也没发生一样。你知道为什么吗?
      • @BjorneX 你能从命令行而不是 Visual Studio 试试吗?如果出现此类错误,最好减少可能的问题来源并尽可能靠近解释器。
      • Oo.oO 现在我都下载了,但是一旦我输入命令,仍然没有弹出窗口或任何东西
      猜你喜欢
      • 2021-10-20
      • 1970-01-01
      • 2016-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-29
      • 2018-08-30
      • 2020-12-23
      相关资源
      最近更新 更多