Python程序教程

您现在的位置是:首页 >  Python

当前栏目

python画图函数

python,画图,函数
2025-04-01 16:27:53 时间

大家好,又见面了,我是你们的朋友全栈君。

python画图函数

1.使用turtle模块

import turtle

2.画笔状态函数

turtle.penup()		#提起画笔,移动之后不会绘制图形
turtle.pendown()		#放下画笔,移动之后绘制图形,与penup()配套使用
turtle.pensize(width)		#设置画笔线条的粗细,()中填数字,数字越大,笔越粗
turtle.speed()			#设置画笔的速度,参数在0~10之间
turtle.pencolor()		#设置笔的颜色
turtle.color()		#设置笔的颜色和填充颜色
turtle.begin_fill()		#填充图形前调用该函数
turtle.end_fill()		#填充图形完毕
turtle.filling()		#返回填充的状态,True为填充,False为未填充
turtle.hideturtle()		#隐藏画笔
turtle.showturtle()		#展现画笔
turtle.isvisible()		#查看画笔是是否可见,如果可见,返回True

3.窗口函数

turtle.setup(width,height,startx,starty)			
"""
wideth:窗口宽度。若为整数则代表像数值,小数则代表窗口宽度和屏幕的比例
height:窗口高度。若为整数则代表像数值,小数则代表窗口宽度和屏幕的比例
startx:窗口左侧和屏幕左侧的像素距离。值为None,窗体位于屏幕水平中央
starty:窗口右侧和屏幕右侧的像素距离。值为None,窗口位于屏幕水平中央
"""
turtle.screensize()			#设置窗口的高度,宽度和背景颜色
turtle.clear()			#清空当前窗口,但不改变画笔的状态
turtle.reset()			#清空当前窗口,同时重置位置等状态为默认值

4.画笔运动函数

turtle.forword()			#沿当前方向运动()个像素距离
turtle.backword()			#沿当前相反的方向运动()个像素距离
turtle.right(angle)			#向右旋转angle角度
turtle.left(angle)			#向左旋转angle角度
turtle.setheading(angle)			#设置当前朝向angle的方向
turtle.goto(x,y)			#移动到(x,y)坐标处
turtle.setx(x)			#画笔的横坐标修改到x,纵坐标不变
turtle.sety(y)			#画笔的纵坐标修改到y,横坐标不变
turtle.home()			#设置画笔位置为原点,方向朝向东
turtle.circle(r,extent=None,step=None)			#绘制一个半径为r,角度为e的圆或者弧,steps为该圆的内切多边形,边数为steps
turtle.undo()			#撤销画笔最后一步的操作
turtle.dot(radius,e)			#绘制一个半径为r,颜色为e的圆点
turtle.done()			#结束绘画

5.turtle库的练习 例一:一个for循环,打出一个好看的图形

import turtle
turtle.speed(0)
turtle.color("red")
for i in range(260):
    turtle.forward(i)
    turtle.right(160)
turtle.done()

例二:用turtle程序来模仿在各自里随意行走,从中心位置开始,然后在边缘处的某点停下来。

#格子
import turtle
import random
turtle.speed(0)
row=8  #行数
col=8  #列数
cell=40
uplimit=(row//2)*cell  #上限
downlimit=-(row//2)*cell  #下限
leftlimit=-(col//2)*cell  #左限
rightlimit=(col//2)*cell  #右限
turtle.penup()
turtle.goto(leftlimit,uplimit)
turtle.pendown()
"""  
#方法一 每小格法
for i in range(1,9):
        for j in range(1,9):
                for k in range(1,5):
                        turtle.forward(cell)
                        turtle.right(90)
                turtle.forward(cell)
        turtle.penup()
        turtle.goto(leftlimit,uplimit-i*cell)
        turtle.pendown()
#turtle.done() 
"""
# 方法二 横竖线法
for i in range(1,row+2):
        turtle.forward(col*cell)
        turtle.penup()
        turtle.goto(leftlimit,uplimit-i*cell)
        turtle.pendown()
turtle.up()
turtle.goto(leftlimit,uplimit)
turtle.pendown()
turtle.right(90)
for j in range(1,col+2):
        turtle.forward(row*cell)
        turtle.penup()
        turtle.goto(leftlimit+j*cell,uplimit)
        turtle.pendown()

turtle.penup()
turtle.goto(0,0)
turtle.pendown()
turtle.pensize(5)
x=0
y=0
while x!=leftlimit and y!=uplimit and y!=downlimit and x!=rightlimit:
    direction=random.randint(0,3)  #0东  1北  2西   3南
    if direction==0:
        x+=cell
    if direction==1:
        y+=cell
    if direction==2:
        x-=cell
    if direction==3:
        y-=cell
    turtle.setheading(direction*90)
    turtle.forward(cell)
    print(x,y)
turtle.done()
turtle.hideturtle()

例三:不用write函数,打出lay

import turtle
#字母L
turtle.penup()
turtle.goto(-200,150)
turtle.pendown()
turtle.pensize(10)
turtle.pencolor("yellow")
turtle.goto(-220,-100)
turtle.right(90)
turtle.circle(50,150)
# 字母a
turtle.penup()
turtle.goto(-50,20)
turtle.pendown()
turtle.pencolor("red")
turtle.left(70)
turtle.circle(55,280)
turtle.penup()
turtle.goto(-50,20)
turtle.pendown()
turtle.right(170)
turtle.circle(70,90)
# 字母y
turtle.penup()
turtle.goto(20,150)
turtle.pendown()
turtle.pencolor("blue")
turtle.goto(10,70)
turtle.right(50)
turtle.circle(50,60)
turtle.penup()
turtle.goto(100,150)
turtle.pendown()
turtle.goto(80,-130)
turtle.left(90)
turtle.circle(60,-100)
turtle.right(30)
turtle.circle(30,-60)
turtle.goto(150,0)
turtle.penup()
turtle.goto(45,30)
turtle.pendown()
turtle.left(130)
turtle.circle(50,70)
turtle.hideturtle()
turtle.done()

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/136393.html原文链接:https://javaforall.cn