【Python2】使用python中的turtle模块学习海龟绘图(有趣的python初体验)

海龟绘图

海龟绘图是Python中非常流行的绘图工具。1966年,西蒙·派珀特 博士发明了一种专门给儿童学习编程的语言——LOGO语言。它的特色就是通过编程指挥一只小海龟在屏幕上绘图。

而Python内置了海龟绘图模块,复制了原始的海龟绘图的所有功能。

          在下面的程序中,大家会发现一个turtle.mainloop(),在海龟绘图中最后使用此方法可以让绘图窗口不要在绘制结束后关闭

          Tkinter模块("Tk 接口")是Python的标准Tk GUI工具包的接口.Tk和Tkinter可以在大多数的Unix平台下使用,同样可以应用在Windows和Macintosh系统里.Tk8.0的后续版本可以实现本地窗口风格,并良好地运行在绝大多数平台中

在此文链接中有较为好理解的解释:https://blog.csdn.net/San__Qi/article/details/100568682

Python中tkinter的mainloop函数实质

正文
以下是一般大多数tkinter程序员都共有的步骤,它的代码做了以下这些事情:
1.从痛苦inter模块中加载一个组件类。
2.创建该组件类的实例为标签类
3.在父组件中打包新标签。
4.调用主循环,显示窗口,同时开始tkinter的事件循环。
mainloop方法最后执行,将标签显示在屏幕,进入等待状态(注:若组件未打包,则不会在窗口中显示),准备响应用户发起的GUI事件。在mainloop函数中,tkinter内部会监控这些事件,如键盘活动,鼠标单击等。事实上,tkinter的mainloop函数与下面的Python伪代码实质是一样的:

def mainloop():
    while the main window has not been closed:
        if an event has occurred:
            run the associated event handler function


由于这个事件模型,只要GUI还在屏幕上,mainloop调用就不会返回执行代码。当我们写大型代码时,调用mainloop后唯一的做法就是注册调用管理器来响应事件。直到满足终止条件时,调用Tk()实例中的quit来打断mainloop的执行。事实上sys.exit函数也可以用来退出GUI,它通过抛出一个异常进而退出程序,该异常是可以进行捕获的(千万不要采用os._exit方法,它同样可以退出整个程序,但它不会执行清理动作,并且它不能被捕获)。窗口(如Tk根窗口,TopLevel实例)中的destroy函数也可以用来关闭GUI,不过我们通常不采用该方法,但它与quit函数不同,当程序中有多个Tk根窗口时,destroy只有在最后一个根窗口被关闭后才会退出GUI。

当然,在mainloop中我们也可以对这些事件进行过滤等操作,不过这与本文无太大关联。

总结自《Python编程》第四版----O’REILLY著。

turtle模块里的方法

  • 导入模块

    在Python中使用模块的时候需要先导入,例如使用海龟绘图的时候就需要import turtle来进行导入

    扫描二维码关注公众号,回复: 10119265 查看本文章
  • ’召唤‘海龟

    海龟绘图中小海龟(画笔)的初始形状是一个箭头,我们可以使用turtle里面的shape方法来改变它的形状。

    import turtle
    
    turtle.shape('turtle')


    这样就把它的形状变成了一只小乌龟。
    其他的形状还有:“arrow”, “circle”, “square”, “triangle”, “classic”.

  • 前进

    使用forward方法来控制小海龟朝面向的方向前进

    import turtle
    turtle.shape('turtle')
    turtle.forward(100)

  • 画圆

    使用circle方法来命令小海龟绘制圆形
     

    import turtle
    turtle.shape('turtle')
    turtle.circle(50)
     

    其中circle后面括号里的数字为圆的半径大小,单位为像素

  • 转弯

    使用left方法可以控制小海龟朝左边转向

    import turtle
    turtle.shape('turtle')
    turtle.left(45)
    turtle.forward(100)
     

    这段代码就可以控制小海龟先朝左边转45度,再前进绘制直线。

  • 绘制正方形

    import turtle
    turtle.shape('turtle')
    turtle.forward(100)
    turtle.left(90)
    turtle.forward(100)
    turtle.left(90)
    turtle.forward(100)
    turtle.left(90)
    turtle.forward(100)
    turtle.left(90)
    turtle.mainloop()

  • 绘制雪人

    import turtle
    turtle.shape('turtle')
    turtle.circle(50)
    turtle.left(180)
    turtle.circle(100)
    turtle.mainloop()

  • forward前进 -- backward后退

    import turtle
    turtle.shape('turtle')
    turtle.backward(100)

  • left左转 -- right右转

    import turtle
    turtle.right(45)
    turtle.forward(100)

  • 红色正方形

    import turtle
    
    turtle.pensize(4)
    turtle.pencolor('red')
    
    turtle.forward(100)
    turtle.right(90)
    turtle.forward(100)
    turtle.right(90)
    turtle.forward(100)
    turtle.right(90)
    turtle.forward(100)
    
    turtle.mainloop()

  • 绘制国旗

    """
    用Python的turtle模块绘制国旗
    """
    import turtle
    
    
    def draw_rectangle(x, y, width, height):
        """绘制矩形"""
        turtle.goto(x, y)
        turtle.pencolor('red')
        turtle.fillcolor('red')
        turtle.begin_fill()
        for i in range(2):
            turtle.forward(width)
            turtle.left(90)
            turtle.forward(height)
            turtle.left(90)
        turtle.end_fill()
    
    
    def draw_star(x, y, radius):
        """绘制五角星"""
        turtle.setpos(x, y)
        pos1 = turtle.pos()
        turtle.circle(-radius, 72)
        pos2 = turtle.pos()
        turtle.circle(-radius, 72)
        pos3 = turtle.pos()
        turtle.circle(-radius, 72)
        pos4 = turtle.pos()
        turtle.circle(-radius, 72)
        pos5 = turtle.pos()
        turtle.color('yellow', 'yellow')
        turtle.begin_fill()
        turtle.goto(pos3)
        turtle.goto(pos1)
        turtle.goto(pos4)
        turtle.goto(pos2)
        turtle.goto(pos5)
        turtle.end_fill()
    
    
    def main():
        """主程序"""
        turtle.speed(12)
        turtle.penup()
        x, y = -270, -180
        # 画国旗主体
        width, height = 540, 360
        draw_rectangle(x, y, width, height)
        # 画大星星
        pice = 22
        center_x, center_y = x + 5 * pice, y + height - pice * 5
        turtle.goto(center_x, center_y)
        turtle.left(90)
        turtle.forward(pice * 3)
        turtle.right(90)
        draw_star(turtle.xcor(), turtle.ycor(), pice * 3)
        x_poses, y_poses = [10, 12, 12, 10], [2, 4, 7, 9]
        # 画小星星
        for x_pos, y_pos in zip(x_poses, y_poses):
            turtle.goto(x + x_pos * pice, y + height - y_pos * pice)
            turtle.left(turtle.towards(center_x, center_y) - turtle.heading())
            turtle.forward(pice)
            turtle.right(90)
            draw_star(turtle.xcor(), turtle.ycor(), pice)
        # 隐藏海龟
        turtle.ht()
        # 显示绘图窗口
        turtle.mainloop()
    
    
    if __name__ == '__main__':
        main()

  • 绘制小猪佩奇

    """
    绘制小猪佩奇
    """
    from turtle import *
    
    
    def nose(x,y):
        """画鼻子"""
        penup()
        # 将海龟移动到指定的坐标
        goto(x,y)
        pendown()
        # 设置海龟的方向(0-东、90-北、180-西、270-南)
        setheading(-30)
        begin_fill()
        a = 0.4
        for i in range(120):
            if 0 <= i < 30 or 60 <= i <90:
                a = a + 0.08
                # 向左转3度
                left(3)
                # 向前走
                forward(a)
            else:
                a = a - 0.08
                left(3)
                forward(a)
        end_fill()
        penup()
        setheading(90)
        forward(25)
        setheading(0)
        forward(10)
        pendown()
        # 设置画笔的颜色(红, 绿, 蓝)
        pencolor(255, 155, 192)
        setheading(10)
        begin_fill()
        circle(5)
        color(160, 82, 45)
        end_fill()
        penup()
        setheading(0)
        forward(20)
        pendown()
        pencolor(255, 155, 192)
        setheading(10)
        begin_fill()
        circle(5)
        color(160, 82, 45)
        end_fill()
    
    
    def head(x, y):
        """画头"""
        color((255, 155, 192), "pink")
        penup()
        goto(x,y)
        setheading(0)
        pendown()
        begin_fill()
        setheading(180)
        circle(300, -30)
        circle(100, -60)
        circle(80, -100)
        circle(150, -20)
        circle(60, -95)
        setheading(161)
        circle(-300, 15)
        penup()
        goto(-100, 100)
        pendown()
        setheading(-30)
        a = 0.4
        for i in range(60):
            if 0<= i < 30 or 60 <= i < 90:
                a = a + 0.08
                lt(3) #向左转3度
                fd(a) #向前走a的步长
            else:
                a = a - 0.08
                lt(3)
                fd(a)
        end_fill()
    
    
    def ears(x,y):
        """画耳朵"""
        color((255, 155, 192), "pink")
        penup()
        goto(x, y)
        pendown()
        begin_fill()
        setheading(100)
        circle(-50, 50)
        circle(-10, 120)
        circle(-50, 54)
        end_fill()
        penup()
        setheading(90)
        forward(-12)
        setheading(0)
        forward(30)
        pendown()
        begin_fill()
        setheading(100)
        circle(-50, 50)
        circle(-10, 120)
        circle(-50, 56)
        end_fill()
    
    
    def eyes(x,y):
        """画眼睛"""
        color((255, 155, 192), "white")
        penup()
        setheading(90)
        forward(-20)
        setheading(0)
        forward(-95)
        pendown()
        begin_fill()
        circle(15)
        end_fill()
        color("black")
        penup()
        setheading(90)
        forward(12)
        setheading(0)
        forward(-3)
        pendown()
        begin_fill()
        circle(3)
        end_fill()
        color((255, 155, 192), "white")
        penup()
        seth(90)
        forward(-25)
        seth(0)
        forward(40)
        pendown()
        begin_fill()
        circle(15)
        end_fill()
        color("black")
        penup()
        setheading(90)
        forward(12)
        setheading(0)
        forward(-3)
        pendown()
        begin_fill()
        circle(3)
        end_fill()
    
    
    def cheek(x,y):
        """画脸颊"""
        color((255, 155, 192))
        penup()
        goto(x,y)
        pendown()
        setheading(0)
        begin_fill()
        circle(30)
        end_fill()
    
    
    def mouth(x,y):
        """画嘴巴"""
        color(239, 69, 19)
        penup()
        goto(x, y)
        pendown()
        setheading(-80)
        circle(30, 40)
        circle(40, 80)
    
    
    def setting():
        """设置参数"""
        pensize(4)
        # 隐藏海龟
        hideturtle()
        colormode(255)
        color((255, 155, 192), "pink")
        setup(840, 500)
        speed(10)
    
    
    def main():
        """主函数"""
        setting() 
        nose(-100, 100)
        head(-69, 167)
        ears(0, 160)
        eyes(0, 140)
        cheek(80, 10)
        mouth(-20, 30)
        done()
    
    
    if __name__ == '__main__':
        main()

发布了46 篇原创文章 · 获赞 18 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/IGGIRing/article/details/105067711