Python起步之turtle库应用实例
2017-07-26 17:47
232 查看
1、运用turtle库函数实现绘制不同几何图形,并做填充处理:
# TurtleTest.py
import turtle
def 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.py
from tkinter import *
import time
def 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()
执行结果:
# TurtleTest.py
import turtle
def 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.py
from tkinter import *
import time
def 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()
执行结果:
相关文章推荐
- 【实战】Python读写Excel实例应用
- Python应用03 使用PyQT制作视频播放器实例
- python子进程模块subprocess详解与应用实例 之三
- python子进程模块subprocess详解与应用实例 之二
- 零基础学python-17.4 参数匹配应用实例
- python编写脚本应用实例
- 零基础学python-17.4 参数匹配应用实例
- Python中摘要算法MD5,SHA1简介及应用实例代码
- [Python]网络爬虫(四):Opener与Handler的介绍和实例应用
- Python实例应用
- python子进程模块subprocess详解与应用实例 之三
- Python网络编程Socket应用实例
- python实例:公司应用(一)
- Python应用03 使用PyQT制作视频播放器实例
- python中zip()方法应用实例分析
- python子进程模块subprocess详解与应用实例 之二
- Python起步之面向对象初步应用(烤地瓜)
- Python实例应用
- python实例:公司应用(二)
- [Python]网络爬虫(四):Opener与Handler的介绍和实例应用