Python中turtle库的使用
Turtle图形库
Turtle库是Python内置的图形化模块,属于标准库之一,位于Python安装目录的lib文件夹下,常用函数有以下几种:
- 画笔控制函数
penup()
:抬起画笔;pendown()
:落下画笔;pensize(width)
:画笔宽度;pencolor(color)
:画笔颜色;
- 运动控制函数
forward(d)/fd(d)
:直行d个像素;circle(r, extent = None)
:绘制半径为r,角度为extent的弧形,圆心默认在海龟左侧距离r的位置;
- 方向控制函数
setheading(angle)/seth(angle)
:改变前进方向;left(angle)
:海龟左转;right(angle)
:海龟右转;
Turtle库的使用
#coding=utf-8
#绘制蟒蛇
import turtle
turtle.penup()
turtle.pencolor("red")
turtle.forward(-250)
turtle.pendown()
turtle.pensize(10)
turtle.right(45)
for i in range(4):
turtle.circle(40, 80)
turtle.circle(-40, 80)
turtle.circle(40, 80 / 2)
turtle.fd(40)
turtle.circle(16, 180)
turtle.fd(40 * 2 / 3)
turtle.done()
结果
#coding=utf-8
# 绘制五角星
import turtle
turtle.pensize(5)
turtle.pencolor("red")
turtle.forward(200)
for i in range(4):
turtle.right(144)
turtle.fd(200)
turtle.done()
结果
#绘制时钟
# coding=utf-8
import turtle as tt
from datetime import *
# 当前日期属于一周的第几天
def Week(t):
week = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"]
return week[t.weekday()]
# 获取当前时间
def Date(t):
y = t.year
m = t.month
d = t.day
cur_hour = t.hour;
cur_min = t.minute;
cur_sec = t.second;
return "%s-%d-%d %d:%02d:%02d" % (y, m, d, cur_hour, cur_min, cur_sec)
# 移动画笔,距离为distance
def movePen(distance):
tt.penup()
tt.pensize(5)
tt.pencolor("blue")
tt.fd(distance)
tt.pendown()
# 绘制表针
def makeHands(name, length):
# 清空窗口,重置turtule状态为初始状态
tt.reset()
movePen(-length * 0.1)
# 开始记录多边形的顶点
tt.begin_poly()
tt.fd(length * 1.1)
# 停止记录多边形的顶点
tt.end_poly()
# 返回记录的多边形
handForm = tt.get_poly()
tt.register_shape(name, handForm)
# 初始化
def initial():
global secHand, minHand, hurHand, printer
# 重置方向向北(上),正角度为顺时针
tt.mode("logo")
# 建立并初始化表针
makeHands("secHand", 180)
makeHands("minHand", 150)
makeHands("hurHand", 110)
secHand = tt.Turtle()
secHand.shape("secHand")
minHand = tt.Turtle()
minHand.shape("minHand")
hurHand = tt.Turtle()
hurHand.shape("hurHand")
for hand in secHand, minHand, hurHand:
hand.shapesize(1, 1, 4)
hand.speed(0)
# 输出文字
printer = tt.Turtle()
# 隐藏画笔
printer.hideturtle()
printer.penup()
# 绘制表盘外框
def drawClock(R):
# 清空窗口,重置turtule状态为初始状态
tt.reset()
# 画笔尺寸
tt.pensize(5)
for i in range(60):
movePen(R)
if i % 5 == 0:
tt.fd(20)
movePen(-R - 20)
movePen(R + 20)
if i == 0:
# 写文本
tt.write(int(12), align="center", font=("Consolas", 14, "bold"))
elif i == 30:
movePen(25)
tt.write(int(i / 5), align="center", font=("Consolas", 14, "bold"))
movePen(-25)
elif (i == 25 or i == 35):
movePen(20)
tt.write(int(i / 5), align="center", font=("Consolas", 14, "bold"))
movePen(-20)
else:
tt.write(int(i / 5), align="center", font=("Consolas", 14, "bold"))
movePen(-R - 20)
else:
# 绘制指定半径和颜色的点
tt.dot(5, "red")
movePen(-R)
tt.right(6)
# 表针的动态显示
def handsMove():
t = datetime.today()
second = t.second + t.microsecond * 0.000001
minute = t.minute + second / 60.0
hour = t.hour + minute / 60.0
secHand.seth(6 * second)
minHand.seth(6 * minute)
hurHand.seth(30 * hour)
tt.tracer(False)
printer.fd(65)
tt.pencolor("green")
printer.write(Week(t), align="center", font = ("黑体", 14))
printer.back(130)
printer.write(Date(t), align="center", font = ("Consolas", 14))
# 设置当前画笔位置为原点,方向朝东
printer.home()
tt.tracer(True)
# 经过100ms后继续调用handsMove函数
tt.ontimer(handsMove, 100)
# 调用定义的函数,打开和关闭动画,为更新图纸设置延迟;
tt.tracer(False)
initial()
drawClock(200)
tt.tracer(True)
handsMove()
tt.mainloop()
结果
欢迎关注微信公众号:村雨1943;创作不易,未经同意,转载请注明出处~
Python中turtle库的使用的更多相关文章
- Python使用turtle库与random库绘制雪花
记录Python使用turtle库与random库绘制雪花,代码非常容易理解,画着玩玩还是可以的. 完整代码如下: 效果图如下:
- Python之turtle库-小猪佩奇
Python之turtle库-小猪佩奇 #!/usr/bin/env python # coding: utf-8 # Python turtle库官方文档:https://docs.python.o ...
- python中requests库使用方法详解
目录 python中requests库使用方法详解 官方文档 什么是Requests 安装Requests库 基本的GET请求 带参数的GET请求 解析json 添加headers 基本POST请求 ...
- Python中第三方库Requests库的高级用法详解
Python中第三方库Requests库的高级用法详解 虽然Python的标准库中urllib2模块已经包含了平常我们使用的大多数功能,但是它的API使用起来让人实在感觉不好.它已经不适合现在的时代, ...
- Python中turtle绘图学习笔记和实例
一.既然本次讲的主角是turtle函数库,那肯定得先了解一下它是什么 turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x.纵轴为y的坐标系原点,(0,0)位 ...
- 第四章 python的turtle库的运用
我们可以尝试用python的自带turtle库绘制一条蟒蛇 首先我们设计一下蟒蛇的基本形状 我们先把这段蟒蛇绘制的实例代码贴出来,各位可以在自己的本地运行一下看看效果,然后我们再继续分析代码: 1 # ...
- Python中cv2库和matplotlib库色彩空间排布不一致
今天在python中读如图片时发现以下问题: 1.在from matplotlib import pyplot as plt之后,再import cv2 cv2.imshow()不能正常使用,还不知道 ...
- Python 中拼音库 PyPinyin 的用法【华为云技术分享】
[摘要] 最近碰到了一个问题,项目中很多文件都是接手过来的中文命名的一些素材,结果在部署的时候文件名全都乱码了,导致项目无法正常运行. 后来请教了一位大佬怎么解决文件名乱码的问题,他说这个需要正面解决 ...
- python使用turtle库绘制奥运五环
效果图: #奥运五环 import turtle turtle.setup(1.0,1.0) #设置窗口大小 turtle.title("奥运五环") #蓝圆 turtle.pen ...
随机推荐
- Ubuntu安装邮件服务器
Ubuntu搭建邮件服务器 此文我们使用Postfix来搭建邮箱服务器,Postifx是一个SMTP服务器.SMTP服务器也被称为MTA(message transfer agent) 一.安装pos ...
- java.lang.ClassNotFoundException: org.apache.jsp.error_jsp
缺少jar包 第一个:standard-1.1.2.jar 第二个:jstl-1.2.jar
- 微信小程序带cookie的request请求代码封装(小程序使用session)
微信小程序带cookie的request请求可,以使服务端知道是同一个客户端请求. session_id会不变,从而很好的使用服务端的session. 写一个工具函数,直接导入使用即可,接口同 wx. ...
- python中list和dict
字典(Dictionary)是一种映射结构的数据类型,由无序的“键-值对”组成.字典的键必须是不可改变的类型,如:字符串,数字,tuple:值可以为任何python数据类型. 1.新建字典 1 2 3 ...
- 给Oracle字段和表加注释
给字段加注释 comment on column testtb17.AGE is '年龄';comment on column testtb17.CREATEDTIME is '创建时间';comme ...
- 【转】自动化框架中引入ExtentReport美化报告
本文链接:https://blog.csdn.net/qq_30353203/article/details/82023922一.先引入三个依赖包 <dependency> <gro ...
- idea创建类,接口,枚举等如何设置注释
进入设置: File -> Settings 依次选择: Editor -> File and Code Templates -> Files -> Class (根据需要 ...
- Tosca 给定义变量,取内容放到变量里
可以在TOOLS里 buffer viewer里面搜索查自己的变量
- Tosca case status PLANNED,IN-WORK,COMPLETED 对应的图标
#PLANNED #IN-WORK #COMPLETED
- 主外键 设置 on update cascade 和on delete cascade 的区别
on update cascade 和on delete cascade 的区别 这是数据库外键定义的一个可选项,用来设置当主键表中的被参考列的数据发生变化时,外键表中响应字段的变换规则的.updat ...