【问题标题】:How to make canvas bigger in the python turtle module如何在 python turtle 模块中使画布更大
【发布时间】:2015-10-02 00:40:17
【问题描述】:

我用python做了一个乌龟画程序,但是我画乌龟的画布不够大。我正在尝试使此画布更大,以便我可以在页面上放置更多内容并使内容更大。我正在 trinket.io 中对此进行编程。

【问题讨论】:

    标签: python size turtle-graphics


    【解决方案1】:

    我正在 trinket.io 中对此进行编程。

    那是你的问题——不幸的是,这在 trinket.io 中是不可能的。

    Trinket.io 不支持所有 turtle 方法。你可以阅读哪些是支持的here;我认为其余的不受支持。

    这将适用于您的本地 python 解释器:

    import turtle
    
    print(turtle.Screen().screensize()) # returns (400,300) for me
    

    但这会在 Trinket.io 中失败,并显示如下消息:

    > AttributeError: 'Screen' object has no attribute 'screensize' on line 3 in main.py
    

    文档暗示支持 turtle.setup(),但似乎不支持,因为这将在您的本地 python 解释器上运行,并且在 trinket.io 中失败。

    import turtle
    
    turtle.setup(500,500)
    

    我在 trinket.io 中唯一能做的就是能够通过以下方式返回(未设置)尺寸:

    print(turtle.window_height())
    print(turtle.window_width())
    

    【讨论】:

    【解决方案2】:

    看起来你可以使用:

    import turtle
    
    screen = turtle.Screen()
    # this assures that the size of the screen will always be 400x400 ...
    screen.setup(500, 500)
    tina = turtle.Turtle()
    tina.goto(200,200)
    tina.goto(-200,-200)
    tina.goto(-200,200)
    tina.goto(200,-200)
    

    这是我的trinket :-)

    【讨论】:

      【解决方案3】:
      import turtle
      turtle.screensize(canvwidth=None, canvheight=None, bg=None)
      tina = turtle.Turtle()
      tina.shape('turtle')
      your_name = input("What is your name")
      
      tina.penup()
      tina.forward(20)
      tina.write("Why, hello there, " + your_name + "!", font=("Arial", 12, "normal"))
      tina.backward(20)
      tina.color("green")
      tina.left(90)
      tina.forward(100)
      tina.right(90)
      tina.goto(-65, 50)
      tina.pendown()
      tina.pencolor("red")
      tina.forward(50)
      tina.right(50)
      tina.forward(50)
      tina.right(100)
      tina.forward(55)
      tina.left(50)
      tina.forward(55)
      tina.penup()
      tina.forward(30)
      tina.pendown()
      tina.dot(10)
      tina.penup()
      tina.goto(-100, 100)
      color = input("What color is the question mark")
      try:
        if color == ("red"):
          tina.write("Your are correct " + your_name + "!", font=("Arial", 20, "normal"))
          tina.backward(20)
        elif color == ("green" or "Green"):
          tina.left(90)
          tina.write("Sorry, It is actually Red", font=("Arial", 12, "normal"))
          tina.forward(100)
        elif color == ("black" or "Black"):
          tina.write("Sorry, Its is actually Red", font=("Arial", 12, "normal"))
          tina.backward(20)
        elif color == ("purple" or "Purple"):
          tina.write("Sorry, It is actually Red", font=("Arial", 12, "normal"))
          tina.backward(20)
        elif color == ("blue" or "Blue"):
          tina.write("Sorry, It is actually Red", font=("Arial", 12, "normal"))
          tina.backward(20)
      except:
        tina.backward(20)
        tina.write("Sorry, but that isn't a color", font=("Arial", 12, "normal"))
        tina.backward(20)
      

      【讨论】:

      • 这是您问题的答案,还是您当前的代码?如果是后者,您应该将其包含在您的问题中,而不是在答案帖子中。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-23
      • 1970-01-01
      • 2019-07-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多