【问题标题】:python command to turn turtle into imagepython命令将乌龟变成图像
【发布时间】:2016-05-08 23:59:26
【问题描述】:
import turtle
s1 = turtle.Screen()
b = turtle.Turtle()
b.shape("turtle")
b.color("blue")
b.speed(9)
s1.bgpic("grass.gif")


########## turtles
t1 = turtle.Turtle()
t1.shape('circle')
t1.pu()
t1.goto(100,200)

t2 = turtle.Turtle()
t2.shape('circle')
t2.pu()
t2.goto(-100,-200)

t3 = turtle.Turtle()
t3.shape('circle')
t3.pu()
t3.goto(200,-200)

t4 = turtle.Turtle()
t4.shape('circle')
t4.pu()
t4.goto(0,200)

t5 = turtle.Turtle()
t5.shape('circle')
t5.pu()
t5.goto(400,300)

用于将您的乌龟变成下载的 gif 图像的 python turtle 命令是什么?我需要将 t1 - t5 更改为 gif 图像而不是圆圈。

【问题讨论】:

    标签: python turtle-graphics


    【解决方案1】:

    我使用以下代码将图像导入程序:

    import turtle
    
    wn = turtle.Screen()
    wn.bgcolor('white')
    
    your_image = r"C:\Users\your_name\Pictures\your_image.gif"
    
    wn.addshape(your_image)
    

    要将乌龟变成 .gif 图像,请制作添加了形状的图像。

    your_turtle.shape(your_image)
    

    【讨论】:

      【解决方案2】:

      对我来说,我想使用 PNG,而不是 GIF。终于找到了答案,把GIFPNG的解决方法记录如下,希望对大家有所帮助。

      t = turtle.Turtle()
      
      # you can change the shape with this command, but you need to tell it what the 'your_shape_name' is.
      t.shape('your_shape_name')
      

      如何设置形状

      首先,您需要register_shape by turtle.Screen()

      等等!开始之前,先看源码,如下:

      # Lib/turtle.py
      
      class TurtleScreen(TurtleScreenBase):
          
          ...
      
          def register_shape(self, name, shape=None):
                      
              if shape is None:
                  # image
                  if name.lower().endswith(".gif"):
                      shape = Shape("image", 
                          self._image(name)  # return ``TK.PhotoImage(file=filename)`
                      )
                  else:
                      raise TurtleGraphicsError("Bad arguments for register_shape.\n"
                                                + "Use  help(register_shape)" )
              elif isinstance(shape, tuple):
                  shape = Shape("polygon", shape)
              ## else shape assumed to be Shape-instance
              self._shapes[name] = shape
              
          ...
      
          addshape = register_shape  # <---- so they are the same
      
      

      好的,然后开始,

      screen = turtle.Screen()
      t = turtle.Turtle()  # a Turtle instance
      # t.shape('your_shape_name')  # you can change the shape with this command, but you need tell it what is 'your_shape_name'
      
      if '1. source is png (or anything that is compatible with `tk.PhotoImage`)':    
          your_shape=turtle.Shape('image', tk.PhotoImage(file='your.png')))  # name must is 'image'   
          screen.register_shape(name='your_cool_shape_name', shape=your_shape)
          t.shape('your_cool_shape_name')
          # t.goto(1, 2)
          
      if '2 source is gif':
          from pathlib import Path
          your_gif = str(Path('.../your.gif'))
          screen.register_shape(your_gif)
          t.shape(your_gif)    
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多