python 3:turtle图形库,绘制简单的图形

turtle说明文档:https://docs.python.org/3/library/turtle.html

  • turtle.forward():向前绘制;
  • turtle.backward():与forward()函数对应,这里是从尾部绘制线条和箭头到头部;
  • turtle.left():向左
  • turtle.right():向右
  • turtle.pu():抬起箭头
  • turtle.pd():放下箭头
  • turtle.pensize():设置线条的粗细;
  • turtle.speed():设置绘制的速度(1-10),1最慢,10最快;
  • turtle.begin_fill():准备开始填充图形;
  • turtle.circle(50,steps=3):circle函数在之前用到过,是画一个半径为radius的圆,这里是扩展,steps表示在半径为50的圆内的内置steps多边形;
  • turtle.end_fill():填充完成;
  • turtle.write(s,font=(“font-name”,font_size,”font_type”)):写文本,s为文本内容,font是字体的参数,里面分别为字体名称,大小和类型;
  • turtle.hideturtle():隐藏箭头;
  • turtle.showturtle():显示箭头
  • turtle.left(angle):逆时针转动箭头方向;
  • turtle.undo():撤销上一个turtle动作;
  • turtle.screensize(w,h):设置turtle窗口的长和宽;
  • turtle.clear():清空turtle窗口,但是turtle的位置和状态不会改变。
  • turtle.reset():清空窗口,重置turtle状态为起始状态;
  • turtle.showturtle():与hideturtle()函数对应;
  • turtle.filling():返回当前是否在填充状态;true为filling,false为not filling;
  • turtle.isvisible():返回当前turtle是否可见。
import wx
import turtle

class MyApp(wx.App):
    def OnInit(self):
        frame = wx.Frame(parent = None,
                         title="Lyn's Tool",
                         size=(600,200),
                         pos=(200,300))    #生成框架窗口
        panel = wx.Panel(frame,-1)
        self.buttonWJX = wx.Button(panel,
                                   -1,
                                   "五角星",
                                   size=(75,25),
                                   pos=(10,100))
        self.Bind(wx.EVT_BUTTON,self.OnButtonWJX,self.buttonWJX)

        self.buttonSJX = wx.Button(panel,
                                   -1,
                                   "三角星",
                                   size=(75,25),
                                   pos=(90,100))
        self.Bind(wx.EVT_BUTTON,self.OnButtonSJX,self.buttonSJX)

        self.buttonZFX = wx.Button(panel,
                                   -1,
                                   "正方形",
                                   size=(75,25),
                                   pos=(170,100))
        self.Bind(wx.EVT_BUTTON,self.OnButtonZFX,self.buttonZFX)
        
        self.buttonTX = wx.Button(panel,
                                   -1,
                                   "梯形",
                                   size=(75,25),
                                   pos=(250,100))
        self.Bind(wx.EVT_BUTTON,self.OnButtonTX,self.buttonTX)

        self.buttonYX = wx.Button(panel,
                                   -1,
                                   "圆形",
                                   size=(75,25),
                                   pos=(330,100))
        self.Bind(wx.EVT_BUTTON,self.OnButtonYX,self.buttonYX)

        self.buttonDBX = wx.Button(panel,
                                   -1,
                                   "多边形",
                                   size=(75,25),
                                   pos=(410,100))
        self.Bind(wx.EVT_BUTTON,self.OnButtonDBX,self.buttonDBX)

        self.buttonTYX = wx.Button(panel,
                                   -1,
                                   "椭圆形",
                                   size=(75,25),
                                   pos=(490,100))
        self.Bind(wx.EVT_BUTTON,self.OnButtonTYX,self.buttonTYX)
        
        frame.Show(True)                       # 显示框架窗口
        return True

####注意,海龟笔头一开始默认是在直角坐标系的(0,0)位置
    
    def OnButtonWJX(self,event):#五角星
        turtle.pu()  #抬起笔头
        turtle.hideturtle()  #隐藏笔头,使得在移动时不可见
        turtle.goto(-300,200)  #向前走到(x,y)位置
        turtle.pd()  #放下笔头
        turtle.showturtle()  #显示笔头,开始绘图
        for i in range(5):
            turtle.forward(150)
            turtle.right(144)
    def OnButtonSJX(self,event):#三角形
        turtle.pu()
        turtle.hideturtle()
        turtle.goto(-100,250)
        turtle.pd()
        turtle.showturtle()
        for i in range(3):
            turtle.forward(150)
            turtle.right(120)
    def OnButtonZFX(self,event):#正方形
        turtle.pu()
        turtle.hideturtle()
        turtle.goto(100,250)
        turtle.showturtle()
        turtle.pd()
        for i in range(4):
            turtle.forward(150)
            turtle.right(90)
    def OnButtonTX(self,event):#梯形
        turtle.pu()
        turtle.hideturtle()
        turtle.goto(-230,0)
        turtle.showturtle()
        turtle.pd()
        turtle.forward(100)
        turtle.right(30)
        turtle.forward(100)
        turtle.right(150)
        turtle.forward(274)
        turtle.right(150)
        turtle.forward(100)
    def OnButtonYX(self,event):#圆形
        turtle.pu()
        turtle.hideturtle()
        turtle.goto(0,0)
        turtle.showturtle()
        turtle.pd()
        for i in range(36):
            turtle.forward(10)
            turtle.right(10)
    def OnButtonDBX(self,event):#多边形
        turtle.pu()
        turtle.hideturtle()
        turtle.goto(150,0)
        turtle.showturtle()
        turtle.pd()
        for i in range(9):
            turtle.forward(40)
            turtle.right(40)
    def OnButtonTYX(self,event):#椭圆
        turtle.pu()
        turtle.hideturtle()
        turtle.goto(-250,-100)
        turtle.showturtle()
        turtle.pd()
        for i in range(10):
            turtle.forward(20)
            turtle.right(9) 
        for i in range(10):
            turtle.forward(10)
            turtle.right(9)
        for i in range(10):
            turtle.forward(20)
            turtle.right(9)
        for i in range(10):
            turtle.forward(10)
            turtle.right(9)
            
if __name__== "__main__":
    app = MyApp()
    app.MainLoop()  #进入消息循环

在以上代码中,使用的对象都是turtle,当然也可以定义一个turtle 对象,如lyn=turtle.Turtle()

turtle 模块(小写的t)提供了一个叫作 Turtle 的函数(大写的T),这个函数会创建一个 Turtle 对象,这个类型是由 turtle 模块来定义的。

海归画图中还有很多函数,可画出各种形状的图案,在以后使用到的时候将再次进行说明。

你可能感兴趣的:(Python学习)