进一步地,我尝试学习了Python 的其中一个非常重要的函数库——turtle库
这是一个用于python绘图的函数库,方便又好用!
对于它的安装,现在我们所用的python 3的系统运用到的指令是:
pip3 install turtle
安装完之后就可以使用它啦~这样就可以开始画画啦~(激动)
一、turtle库基本介绍:
python的turtle库是一个直观有趣的图形绘制的函数库;
turtle库有一个绘制框架:“”小乌龟”有转向,行进的动作,方向有“前进方向”,“后退方向”,“左侧方向”,“右侧方向"等,行进动作有“前进”,“后退”,“旋转”等;
二、开始绘图:
1.建立画布
我们可以用过代码setup()或screensize()建立一个窗口画布,我们就可以在上面作画了!
我现在主要用setup(),其中setup(width,height,startx,starty)
前两个参数是设置屏幕的宽高比例;
后两个是设置窗口在屏幕的的位置;
2.设置画笔
对于画笔,我了解到有几个设置的函数(在引用了from turtle import*的情况下):
pensize()是画笔的大小,pensize(2)即说明画笔大小为2个像素点;
penup()是抬起画笔动作;
pendown()是放下画笔动作;
pencolor()是设置画笔的颜色……
恩,了解了这些,我已经可以开始好好利用我的画笔绘画了!
当然还少不了其他一些指令函数:
forward()/fd() #向前走
backward() #向后走
right(degree) #向右转
left(degree) #向左转
speed(speed) #绘画速度
begin_fill() #开始填充
end_fill() #结束填充……
了解到这些以后,就可以开始实战了!
三、我的作品:
1.画叠加的等边三角形
代码:
from turtle import*
pensize(3)
color('black','white')
penup()
begin_fill()
seth(90)
fd(50)
pendown()
seth(-120)
fd(200)
seth(0)
fd(200)
seth(120)
fd(100)
seth(180)
fd(100)
seth(-60)
fd(100) 
seth(60)
fd(100)
seth(120)
fd(100)
end_fill()
done()
效果:

2.太极!!
代码:
from turtle import*
pensize(2)
color('black','black')
begin_fill()
circle(80)
end_fill()
begin_fill()
seth(180)
color('black','white')
seth(180)
circle(-80,180)
circle(-40,180)
end_fill()
begin_fill()
color('black','black')
circle(40,180)
end_fill()
penup()
seth(90)
fd(30)
seth(0)
pendown()
begin_fill()
color('black','white')
circle(10)
end_fill()
penup()
seth(90)
fd(80)
seth(0)
pendown()
begin_fill()
color('black','black')
circle(10)
end_fill()
done()
效果:

3.国际象棋盘
代码:
from turtle import*
import math
pensize(2)
speed(50)
temp=0
num=0
penup()
seth(120)
fd(150)
pendown()
seth(0)
for i in range(10):
   for i in range(10):
    if(temp%2==0):
       begin_fill()
       color('black','white')
       for i in range(4):
         fd(40)
         right(90)
         fd(40)
         temp+=1
       end_fill()
     else:
       begin_fill()
       color('black','black')
       for i in range(4):
         fd(40)
         right(90)
       fd(40)
       temp+=1
       end_fill()
  if(num%2==0):
    right(90)
     fd(80)
     right(90)
     num+=1
     if(num==10):
        break
   else:
     left(90)
     left(90)
     num+=1
penup()
seth(120)
fd(300)
pendown()
seth(-30)           #写字开始
fd(45)
penup()
seth(194)
fd(70)
pendown()
seth(10)
fd(60)
right(100)
fd(90)
seth(30)
fd(20)                 #偏旁部首写完
penup()
seth(85)
fd(122)
pendown()
seth(0)               #一横
fd(50)
penup()
seth(195)
fd(50)
pendown()
seth(0)               #二横
fd(45)
penup()
seth(195)
fd(70)
pendown()
seth(0)               #三横
fd(80)
penup()
seth(118)
fd(62)
pendown()
seth(-90)
fd(59)                #右上结束
penup()
seth(-148)
fd(20)
pendown()
seth(-90)
fd(90)
seth(90)
fd(90)
seth(0)
fd(39)
right(90)
fd(92)
seth(120)
fd(15)                 #月外框结束
penup()
fd(55)
pendown()
seth(0)
fd(35)
penup()
seth(220)
fd(45)
pendown()
seth(0)
fd(35)                   #写字结束
hideturtle()
end_fill()
done()

(我还特地写了个字~~够酷吧!)
学到这,我深刻认识到原来会用turtle库是多么有用!我甚至觉得可以任意画图了!
Python太棒了!
来源:oschina
链接:https://my.oschina.net/u/4262536/blog/3614414