Python起步之turtle库应用实例

来源:互联网 发布:淘宝客服挂起状态 编辑:程序博客网 时间:2024/05/29 02:12

1、运用turtle库函数实现绘制不同几何图形,并做填充处理:

# TurtleTest.pyimport turtledef main():    turtle.speed(2)        turtle.pensize(3)    turtle.penup()    turtle.goto(-200,-50)    turtle.pendown()    #表示开始做图形填充    turtle.begin_fill()    turtle.color('red')    turtle.circle(40,steps= 3)    #填充结束    turtle.end_fill()    turtle.penup()    turtle.goto(-100,-50)    turtle.pendown()    turtle.begin_fill()    turtle.color("blue")    turtle.circle(40, steps=4)    turtle.end_fill()     turtle.penup()    turtle.goto(0,-50)    turtle.pendown()    turtle.begin_fill()    turtle.color("green")    turtle.circle(40, steps=5)    turtle.end_fill()     turtle.penup()    turtle.goto(100,-50)    turtle.pendown()    turtle.begin_fill()    turtle.color("yellow")    turtle.circle(40, steps=6)    turtle.end_fill()     turtle.penup()    turtle.goto(200,-50)    turtle.pendown()    turtle.begin_fill()    turtle.color("purple")    turtle.circle(40)#未设置步数则认为是绘制圆形    turtle.end_fill()     turtle.color("green")    turtle.penup()    turtle.goto(-100,50)    turtle.pendown()    turtle.write(("Cool Colorful shapes"),#添加文字        font = ("Times", 18, "bold"))#设置文字格式    turtle.hideturtle()#隐藏画笔形状        turtle.done()    if __name__ == '__main__':    main() 
运行结果:


2、利用turtle绘制图形交互界面,以聊天软件为例;

# UITurtle.pyfrom tkinter import *import timedef main():    def sendMsg():#发送消息        strMsg = '我:'+time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())+'\n'        txtMsgList.insert(END,strMsg,'greencolor')        txtMsgList.insert(END,txtMsg.get('0.0',END))        txtMsg.delete('0.0',END)    def cancelMsg():#取消消息        txtMsg.delete('0.0',END)    def sendMsgEvent(event):#发送消息事件        if event.keysym == 'Up':            sendMsg()    #创建窗口    t = Tk()    t.title('与***聊天中')   # t.    #创建frame容器(实际上是将窗口划分成不同的功能区)    frmLT = Frame(width = 500,height = 320,bg = 'white')    frmLC = Frame(width = 500,height = 150,bg = 'white')    frmLB = Frame(width = 500,height = 30)    frmRT = Frame(width = 200,height = 500)    #创建控件    txtMsgList = Text(frmLT)#在父窗口frmLT中创建一个文本对象    txtMsgList.tag_config('greencolor',foreground = '#008C00') #创建tag    txtMsg = Text(frmLC)    txtMsg.bind('<KeyPress-Up>',sendMsgEvent)    btnSend = Button(frmLB,text = '发 送',width = 8,command = sendMsg)    btnCancel = Button(frmLB,text = '取 消',width = 8,command = cancelMsg)    imgInfo = PhotoImage(file = 'python.gif')    lblImage = Label(frmRT,image = imgInfo)    lblImage.image = imgInfo    #窗口布局    frmLT.grid(row = 0,column = 0,columnspan = 2,padx =1,pady = 3)    frmLC.grid(row = 1,column = 0,columnspan = 2,padx =1,pady = 3)    frmLB.grid(row = 2,column = 0,columnspan = 2)    frmRT.grid(row = 0,column = 2,rowspan = 3,padx =2,pady = 3)    #固定大小    frmLT.grid_propagate(0)    frmLC.grid_propagate(0)    frmLB.grid_propagate(0)    frmRT.grid_propagate(0)'''特别注意grid()函数:这个的几何管理器组织在父部件的表状结构中的部件。 语法:widget.grid( grid_options )下面是可能的选项列表:    column : 列放部件,默认为0(最左边的列).    columnspan: 部件占用多少列,默认为1.    ipadx, ipady : 多少部件的像素,水平和垂直方向,部件的边界内.    padx, pady : 多少部件的像素,水平和垂直方向,V的外边界.    row: 该行放小部件;默认的第一行仍然是空的.    rowspan : 多少行的部件占用;默认为1.    sticky : 做什么,如果单元格是比小部件大。默认情况下,用粘='',widget是在其细胞中心。粘可能是字符串连接的零个或多个N,E,S,W,东北,西北,东南,西南,罗盘方向指示部件坚持单元格的两侧和边角.'''    btnSend.grid(row = 3,column = 0)    btnCancel.grid(row = 3,column = 1)    lblImage.grid()    txtMsgList.grid()    txtMsg.grid()    #主事件循环    t.mainloop()if __name__ == '__main__':    main()
执行结果:



原创粉丝点击