turtle库使用教程

您所在的位置:网站首页 turtle教程 turtle库使用教程

turtle库使用教程

#turtle库使用教程| 来源: 网络整理| 查看: 265

turtle简介

turtle库是Python语言中一个很流行的绘制图像的函数库(内置,无需安装),想像一个小乌龟,在一个横轴为x、纵轴为y的坐标系原点(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行的路径上绘制了图形。

导入turtle库(三种方式) import turtle import turtle as t #取一个别名 from turtle import * turtle窗体布局(画布的位置和大小)

在这里插入图片描述

命令说明turtle.setup(width,height[,startx,starty])前两个参数为画布的宽和高,省略后面两个参数则默认在屏幕中心。turtle.screensize(canvwidth, canvheigh[, bg=None])前两个参数为画布的宽和高,第三个参数为背景颜色 turtle.screensize(800,600, "green") turtle.screensize() #返回默认大小(400, 300) turtle.setup(width=0.6,height=0.6) #为小数时表示占据电脑屏幕的比例 turtle.setup(width=800,height=800, startx=100, starty=100) turtle空间坐标体系

海龟默认起始位置在窗体中心(0,0)处,默认海龟面向X轴正方向。

在这里插入图片描述

画笔的属性 命令说明turtle.pensize()设置画笔的宽度turtle.pencolor()没有参数传入,返回当前画笔颜色;传入参数设置画笔颜色,可以是字符串如"green", “red”,也可以是RGB 3元组。turtle.speed(speed)设置画笔移动速度,画笔绘制的速度范围[0,10]整数,数字越大越快。 画笔运动命令 命令说明turtle.forward(distance)向当前画笔方向移动distance像素长度turtle.backward(distance)向当前画笔相反方向移动distance像素长度turtle.right(degree)顺时针移动degree°turtle.left(degree)逆时针移动degree°turtle.pendown()移动时绘制图形,缺省时也为绘制turtle.goto(x,y)将画笔移动到坐标为x,y的位置turtle.penup()提起笔移动,不绘制图形,用于另起一个地方绘制turtle.circle(radiu,extent=None)画圆,半径为正(负),表示圆心在画笔的左边(右边)画圆,extent不为None时画出的为扇形turtle.setx( )将当前x轴移动到指定位置turtle.sety( )将当前y轴移动到指定位置turtle.setheading(angle)设置当前朝向为angle角度turtle. home()设置当前画笔位置为原点,朝向东。tuttle.dot(r)绘制一个指定直径和颜色的圆点turtle.done()程序运行完后不消失 画笔控制命令 命令说明turtle.fillcolor(colorstring)绘制图形的填充颜色turtle.color(color1, color2)同时设置pencolor=color1, fillcolor=color2turtle.filling()返回当前是否在填充状态turtle.begin_fill()准备开始填充图形turtle.end_fill()填充完成turtle.hideturtle()隐藏画笔的turtle形状turtle.showturtle()显示画笔的turtle形状 实例

七巧板

import turtle as t import time colors=["white","black","red","yellow","green","blue","orange","purple","pink"] t.fillcolor(colors[5]) t.begin_fill() t.left(135) t.forward(100) t.left(135) t.forward(100*2**(1/2)) t.home() t.end_fill() t.fillcolor(colors[4]) t.begin_fill() t.left(45) t.forward(100) t.left(135) t.forward(100*2**(1/2)) t.home() t.end_fill() t.fillcolor(colors[6]) t.begin_fill() t.left(45) t.forward(100) t.right(135) t.forward(100*2**(1/2)) t.right(90) t.forward(100*2**(1/2)) t.home() t.end_fill() t.fillcolor(colors[2]) t.begin_fill() t.left(45) t.forward(100) t.right(135) t.forward(50*2**(1/2)) t.right(45) t.forward(50) t.home() t.end_fill() t.fillcolor(colors[7]) t.begin_fill() t.left(45+180) t.forward(100) t.left(135) t.forward(50*2**(1/2)) t.left(45) t.forward(50) t.home() t.end_fill() t.fillcolor(colors[3]) t.begin_fill() t.left(45) t.forward(50) t.right(135) t.forward(50*2**(1/2)) t.home() t.end_fill() t.fillcolor(colors[8]) t.begin_fill() t.left(45+180) t.forward(50) t.left(90) t.forward(50) t.left(90) t.forward(50) t.home() t.end_fill() time.sleep(3)

在这里插入图片描述 时钟

import turtle from datetime import * # 抬起画笔,向前运动一段距离放下 def Skip(step): turtle.penup() turtle.forward(step) turtle.pendown() def mkHand(name, length): # 注册Turtle形状,建立表针Turtle turtle.reset() Skip(-length * 0.1) # 开始记录多边形的顶点。当前的乌龟位置是多边形的第一个顶点。 turtle.begin_poly() turtle.forward(length * 1.1) # 停止记录多边形的顶点。当前的乌龟位置是多边形的最后一个顶点。将与第一个顶点相连。 turtle.end_poly() # 返回最后记录的多边形。 handForm = turtle.get_poly() turtle.register_shape(name, handForm) def Init(): global secHand, minHand, hurHand, printer # 重置Turtle指向北 turtle.mode("logo") # 建立三个表针Turtle并初始化 mkHand("secHand", 135) mkHand("minHand", 125) mkHand("hurHand", 90) secHand = turtle.Turtle() secHand.shape("secHand") minHand = turtle.Turtle() minHand.shape("minHand") hurHand = turtle.Turtle() hurHand.shape("hurHand") for hand in secHand, minHand, hurHand: hand.shapesize(1, 1, 3) hand.speed(0) # 建立输出文字Turtle printer = turtle.Turtle() # 隐藏画笔的turtle形状 printer.hideturtle() printer.penup() def SetupClock(radius): # 建立表的外框 turtle.reset() turtle.pensize(7) for i in range(60): Skip(radius) if i % 5 == 0: turtle.forward(20) Skip(-radius - 20) Skip(radius + 20) if i == 0: turtle.write(int(12), align="center", font=("Courier", 14, "bold")) elif i == 30: Skip(25) turtle.write(int(i/5), align="center", font=("Courier", 14, "bold")) Skip(-25) elif (i == 25 or i == 35): Skip(20) turtle.write(int(i/5), align="center", font=("Courier", 14, "bold")) Skip(-20) else: turtle.write(int(i/5), align="center", font=("Courier", 14, "bold")) Skip(-radius - 20) else: turtle.dot(5) Skip(-radius) turtle.right(6) def Week(t): week = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"] return week[t.weekday()] def Date(t): y = t.year m = t.month d = t.day return "%s年%d月%d日" % (y, m, d) def Tick(): # 绘制表针的动态显示 t = datetime.today() second = t.second + t.microsecond * 0.000001 minute = t.minute + second / 60.0 hour = t.hour + minute / 60.0 secHand.setheading(6 * second) minHand.setheading(6 * minute) hurHand.setheading(30 * hour) turtle.tracer(False) printer.forward(65) printer.write(Week(t), align="center", font=("Courier", 14, "bold")) printer.back(130) printer.write(Date(t), align="center", font=("Courier", 14, "bold")) printer.home() turtle.tracer(True) # 100ms后继续调用tick turtle.ontimer(Tick, 100) def main(): # 打开/关闭龟动画,并为更新图纸设置延迟。 turtle.tracer(False) Init() SetupClock(160) turtle.tracer(True) Tick() turtle.mainloop() if __name__ == "__main__": main()

在这里插入图片描述 爱心

import turtle as t t.setup(800,800) t.speed(8) t.pensize(10) t.hideturtle() t.pencolor("pink") t.left(45) t.forward(80) t.circle(35,210) t.right(150) t.circle(35,210) t.forward(80) t.left(80) t.forward(150) t.circle(82,210) t.right(130) t.circle(82,210) t.forward(130)

在这里插入图片描述 太阳

import turtle as t t.color('red','yellow') t.begin_fill() while True: t.forward(200) t.left(170) if abs(t.pos())


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3