《趣学Python——教孩子学编程》学习笔记第11-12章
第11章 高级海龟作图
11.1 从最基本的正方形开始
最笨的方式画正方形
>>> import turtle
>>> t = turtle.Pen()
>>> t.forward(50)
>>> t.left(90)
>>> t.forward(50)
>>> t.left(90)
>>> t.forward(50)
>>> t.left(90)
>>> t.forward(50)
image
>>> t.reset()
>>> for x in range(0,5):
t.forward(50)
t.left(90)
11.2 画星星
>>> t.reset()
>>> for x in range(1,9):
t.forward(100)
t.left(225)
image
>>> t.reset()
>>> for x in range(1,38):
t.forward(100)
t.left(175)
image
>>> t.reset()
>>> for x in range(1,20):
t.forward(100)
t.left(95)
image
>>> t.reset()
>>> for x in range(1,19):
t.forward(100)
if x % 2 == 0:
t.left(175)
else:
t.left(225)
image
11.3 画汽车
画车身
>>> t.reset()
>>> t.color(1,0,0)
>>> t.begin_fill()
>>> t.forward(100)
>>> t.left(90)
>>> t.forward(20)
>>> t.left(90)
>>> t.forward(20)
>>> t.right(90)
>>> t.forward(20)
>>> t.left(90)
>>> t.forward(60)
>>> t.left(90)
>>> t.forward(20)
>>> t.right(90)
>>> t.forward(20)
>>> t.left(90)
>>> t.forward(20)
>>> t.end_fill()
画第一个轮子
>>> t.color(0,0,0)
>>> t.up()
>>> t.forward(10)
>>> t.down()
>>> t.begin_fill()
>>> t.circle(10)
>>> t.end_fill()
画第二个轮子
>>> t.setheading(0)
>>> t.up()
>>> t.forward(90)
>>> t.right(90)
>>> t.forward(10)
>>> t.setheading(0)
>>> t.begin_fill()
>>> t.down()
>>> t.circle(10)
>>> t.end_fill()
image
11.4 填色
color函数有三个参数,第一个参数指定有多少红色,第二个参数指定有多少绿色,第三个参数指定有多少蓝色。
比如画个黄色的圆:
>>> t.color(1,1,0)
>>> t.begin_fill()
>>> t.circle(50)
>>> t.end_fill()
11.4.1 用来画填色圆形的函数
>>> def mycircle(red, green, blue):
t.color(red, green, blue)
t.begin_fill()
t.circle(50)
t.end_fill()
>>> mycircle(0,1,0)
>>> mycircle(0,0.5,0)
>>> mycircle(1,0,0)
>>> mycircle(0.9,0.5,0.15)
11.4.2 使用纯白和纯黑
>>> mycircle(0,0,0)
>>> mycircle(1,1,1)
11.5 画正方形的函数
>>> t.reset()
>>> def mysquare(size):
for x in range(1,5):
t.forward(size)
t.left(90)
>>> mysquare(50)
>>> mysquare(75)
>>> mysquare(100)
>>> mysquare(125)
11.6 画填色正方形
>>> def mysquare(size,filled):
if filled == True:
t.begin_fill()
for x in range(1,5):
t.forward(size)
t.left(90)
if filled == True:
t.end_fill()
>>> mysquare(50,True)
>>> mysquare(150,False)
11.7 画填好色的星星
>>> def mystar(size, filled):
if filled == True:
t.begin_fill()
for x in range(1,19):
t.forward(size)
if x % 2 == 0:
t.left(175)
else:
t.left(225)
if filled == True:
t.end_fill()
>>> t.color(0.9,0.75,0)
>>> mystar(120,True)
第12章 用tkinter画高级图形
turtle画图太慢了,如果要要在计算机屏幕上快速绘图,就要用Python标准安装程序中自带的tkinter模块。tkinter可以用来创建完整的应用程序,比如简单的子处理软件,还有简单的绘图软件。
12.1 创造一个可以点的按钮
>>> from tkinter import *
>>> tk = Tk()
>>> btn = Button(tk, text="click me")
>>> btn.pack()
from tkinter import * 用from 模块名 import * 可以在不用模块名字的情况下使用模块的内容来。而如果像前面李子中用了import turtle,我们就得用模块的名字才能访问它的内容
>>> import turtle
>>> t = turtle.Pen()
如果用了 import * 就方便了许多
>>> from turtle import *
>>> t = Pen()
绘制一个按钮,每次点击按钮,就打印“hello there”
>>> def hello():
print('hello there')
>>> from tkinter import *
>>> tk = Tk()
>>> btn = Button(tk, text='click me', command=hello)
>>> btn.pack()
>>> hello there
hello there
hello there
hello there
hello there
hello there
12.2 使用具名参数
>>> def person(width, height):
print('I am %s feet wide, %s feet hight' % (width, height))
通常,我们这样调用它:
>>> person(4,3)
使用具名参数,我们可以调用函数并指定每个值赋予哪个参数:
>>> person(height=3, width=4)
12.3 创建一个画图用的画布
创建一个500像素 * 500像素的画布
>>> from tkinter import *
>>> tk = Tk()
>>> canvas = Canvas(tk,width=500,height=500)
>>> canvas.pack()
12.4 画线
>>> from tkinter import *
>>> tk = Tk()
>>> canvas = Canvas(tk,width=500,height=500)
>>> canvas.pack()
>>> from tkinter import *
>>> tk = Tk()
>>> canvas = Canvas(tk, width=500, height=500)
>>> canvas.pack()
>>> canvas.create_line(0,0,500,500)
1
image
create_line 返回1,它是个标志,我们以后再来了解它。
如果我们要用turtle模块做同样的事情,那就需要下面的代码:
>>> import turtle
>>> turtle.setup(width=500, height=500)
>>> t = turtle.Pen()
>>> t.up()
>>> t.goto(-250,250)
>>> t.down()
>>> t.goto(500,-500)
12.5 画盒子
>>> from tkinter import *
>>> tk = Tk()
>>> canvas = Canvas(tk, width=400, height=400)
>>> canvas.pack()
>>> canvas.create_rectangle(10,10,50,50)
1
>>> canvas.create_rectangle(10,10,300,50)
2
>>> canvas.create_rectangle(10,10,50,300)
3
12.5.1 画许多矩形
>>> from tkinter import *
>>> import random
>>> tk = Tk()
>>> canvas = Canvas(tk, width=400, height=400)
>>> canvas.pack()
>>> def random_rectangle(width, height):
x1 = random.randrange(width)
y1 = random.randrange(height)
x2 = x1 + random.randrange(width)
y2 = y1 + random.randrange(height)
canvas.create_rectangle(x1,y1,x2,y2)
>>> for x in range(0,100):
random_rectangle(400,400)
image
12.5.2 设置颜色
>>> from tkinter import *
>>> import random
>>> tk = Tk()
>>> canvas = Canvas(tk,width=400,height=400)
>>> canvas.pack()
>>> def random_rectangle(width,height,fill_color):
x1 = random.randrange(width)
y1 = random.randrange(height)
x2 = random.randrange(x1+random.randrange(width))
y2 = random.randrange(y1+random.randrange(height))
canvas.create_rectangle(x1,y1,x2,y2,fill=fill_color)
>>> random_rectangle(400,400,'green')
>>> random_rectangle(400,400,'red')
>>> random_rectangle(400,400,'blue')
>>> random_rectangle(400,400,'orange')
>>> random_rectangle(400,400,'yellow')
>>> random_rectangle(400,400,'pink')
>>> random_rectangle(400,400,'purple')
>>> random_rectangle(400,400,'violet')
>>> random_rectangle(400,400,'magenta')
>>> random_rectangle(400,400,'cyan')
>>> random_rectangle(400,400,'gray')
>>> random_rectangle(400,400,'#ffd800')
image
调取取色板
>>> import tkinter.colorchooser
>>> tkinter.colorchooser.askcolor()
image
选择一个颜色并按“确定”,会出现一个元组。这个元组中包含了另一个元组,其中三个数字和一个字符串:
>>> tkinter.colorchooser.askcolor()
((255.99609375, 128.5, 64.25), '#ff8040')
元组的第一个元素代表红绿蓝的量,第二个元素是这三个数字的十六进制的值。
>>> from tkinter import *
>>> import random
>>> tk = Tk()
>>> canvas = Canvas(tk,width=400,height=400)
>>> canvas.pack()
>>> def random_rectangle(width,height,fill_color):
x1 = random.randrange(width)
y1 = random.randrange(height)
x2 = random.randrange(x1+random.randrange(width))
y2 = random.randrange(y1+random.randrange(height))
canvas.create_rectangle(x1,y1,x2,y2,fill=fill_color)
>>> c = tkinter.colorchooser.askcolor()
>>> random_rectangle(400,400,c[1])
12.6 画圆弧
>>> from tkinter import *
>>> tk = Tk()
>>> canvas = Canvas(tk, width=400, height=400)
>>> canvas.pack()
>>> canvas.create_arc(10,10,200,100,extent=180,style=ARC)
以上代码中的参数含义:左上角坐标(10,10),右下角坐标(200,100),extent是用来指定圆弧的角度。
>>> from tkinter import *
>>> tk = Tk()
>>> canvas = Canvas(tk,width=400,height=400)
>>> canvas.pack()
>>> canvas.create_arc(10,10,200,80,extent=45,style=ARC)
1
>>> canvas.create_arc(10,80,200,160,extent=90,style=ARC)
2
>>> canvas.create_arc(10,160,200,240,extent=135,style=ARC)
3
>>> canvas.create_arc(10,240,200,320,extent=180,style=ARC)
4
>>> canvas.create_arc(10,320,200,400,extent=359,style=ARC)
5
image
注意:在画最后一个圈时我们用了359度而不是360度,因为tkinter把360度当做0度。
12.7 画多边形
>>> from tkinter import *
>>> tk = Tk()
>>> canvas = Canvas(tk,width=400,height=400)
>>> canvas.pack()
>>> canvas.create_polygon(10,10,100,10,100,110,fill='',outline='black')
1
>>> canvas.create_polygon(200,10,240,30,120,100,140,120,fill='',outline='black')
2
image
12.8 显示文字
>>> from tkinter import *
>>> tk = Tk()
>>> canvas = Canvas(tk,width=400,height=400)
>>> canvas.pack()
>>> canvas.create_text(150,100,text='There once was a man from Toulouse')
1
>>> canvas.create_text(130,120,text='Who rode around on a moose.',fill='red')
2
>>> canvas.create_text(150,150,text='He said,"It\'s my curse,"',font=('Times',15))
3
>>> canvas.create_text(200,200,text='But it could be worse,',font=('Helvetica',20))
4
>>> canvas.create_text(220,250,text='My cousin rides round',font=('Courier',22))
5
>>> canvas.create_text(220,300,text='on a goose.',font=('Helvetica',30))
6
image
12.9 显示图片
用tkinter只能装入GIF图片,也就是扩展名是.gif的图片文件。想要显示其他类型的图片,如PNG和JPG,就需要用到其他的模块
>>> from tkinter import *
>>> tk = Tk()
>>> canvas = Canvas(tk,width=400,height=400)
>>> canvas.pack()
>>> my_image = PhotoImage(file = 'D:\\DataguruPyhton\\gif_error.gif')
canvas.create_image(0,0,anchor=NW,image=my_image)
1
image(0,0)是我们要显示图片的位置,anchor=NW让函数使用左上角(northwest 西北方向)作为画图的起始点,否则会默认用图片的中心作为起始点。
12.10 创建基本的动画
>>> import time
>>> from tkinter import *
>>> tk = Tk()
>>> canvas = Canvas(tk,width=400,height=400)
>>> canvas.pack()
>>> canvas.create_polygon(10,10,10,60,50,35)
1
>>> for x in range(0,60):
canvas.move(1,5,0)
tk.update()
time.sleep(0.05)
canvas.move(1,5,0) 把ID为1的对象,横移5个像素,纵移0个像素
12.11 让对象操作有反应
>>> from tkinter import *
>>> tk = Tk()
>>> canvas = Canvas(tk,width=400,height=400)
>>> canvas.pack()
>>> canvas.create_polygon(10,10,10,60,50,35)
1
>>> def movetriangle(event):
canvas.move(1,5,0)
>>> canvas.bind_all('<KeyPress-Return>',movetriangle)
'2452315417608movetriangle'
canvas.bind_all('<KeyPress-Return>',movetriangle) 第一个参数代表让tkinter监视什么事件,第二个参数代表事件触发时调用哪个函数。在这里<KeyPress-Return>是按下回车键。运行上面的代码后,用鼠标点击画布,然后在键盘上按回车键。
>>> from tkinter import *
>>> tk = Tk()
>>> canvas = Canvas(tk,width=400,height=400)
>>> canvas.pack()
>>> canvas.create_polygon(10,10,10,60,50,35)
1
>>> def movetriangle(event):
if event.keysym == 'Up':
canvas.move(1,0,-3)
elif event.keysym == 'Down':
canvas.move(1,0,3)
elif event.keysym == 'Left':
canvas.move(1,-3,0)
else:
canvas.move(1,3,0)
>>> canvas.bind_all('<KeyPress-Up>',movetriangle)
'2452315417672movetriangle'
>>> canvas.bind_all('<KeyPress-Down>',movetriangle)
'2452275541384movetriangle'
>>> canvas.bind_all('<KeyPress-Left>',movetriangle)
'2452315549640movetriangle'
>>> canvas.bind_all('<KeyPress-Right>',movetriangle)
'2452315586696movetriangle'
12.12 更多使用ID的方法
只要用了画布上面以create_开头的函数,它总会返回一个ID,这个识别编号可以在其他画布的函数中使用,就像move函数一样。但问题是create_不会总是返回1,所以我们可以修改代码把返回值作为一个变量保存,然后使用这个变量(而不是直接使用数字1),那么无论返回值是多少,这段代码都能工作。
>>> from tkinter import *
>>> tk = Tk()
>>> canvas = Canvas(tk,width=400,height=400)
>>> canvas.pack()
>>> mytriangle = canvas.create_polygon(10,10,10,60,50,35,fill='red')
>>> canvas.itemconfig(mytriangle, fill='blue')
>>> canvas.itemconfig(mytriangle, outline='red')
画布上的itemconfig函数可以用来改变形状的某些参数,上面代码用到了填色和轮廓线的颜色。