用turtle(海龟)画图面临的问题就是太慢,即便是以最大的速度也还是太慢了,下面介绍一下Tkinter模块。注意:Tkinter模块在python2中和python3中还是有很大的区别,这里以python2来介绍,主要是一些小的应用。
Tk()是用来创建一个基本的窗口,可以在上面添加东西,比如按钮,输入框,或者用来画图的画布等。
pack() 显示按钮或者对象,没有这句话的话上面想要执行的代码都不会执行。
Canvas()用来创建画布。
create_开头的一堆函数。(每执行一次都会返回一个ID,可根据这个ID对所创建的图形进行操作)
mainloop()用来持续监听。
关于tkColorChooser模块
from tkColorChooser import *
askcolor()
通过上面的两行代码可以获得颜色选择器
create_中用到的一些方法
fill 填充颜色
outline 填充边框的颜色
text 文本域,在添加按钮的时候给按钮设定名称或者是往画布中添加颜色
extent 创建弧形的时候用到,用来设定弧形的角度
PhotoImage 往画布中添加图片的时候用到,把图片加入变量
上述介绍的在下面的代码中都会有涉及,详细看代码
1.创建一个可以点的按钮
#!/usr/bin/python
#coding: utf-8
from Tkinter import *
'''
创建按钮
'''
def fun():
print('点死了')
tk = Tk()
# 创建一个按钮,并让它显示出来
# command为点击该按钮所要做的命令
btn = Button(tk, text = 'click me',command = fun)
btn.pack()
btn.mainloop()
#!/usr/bin/python
#coding: utf-8
'''
创建两条对角线
'''
from Tkinter import *
tk = Tk()
canvas = Canvas(tk, width = 500, height = 500)
canvas.pack()
# 结果返回1,证明画线成功,以后每次都累加
t = canvas.create_line(0, 0, 500, 500)
print(t)
t = canvas.create_line(500, 0, 0, 500)
print(t)
tk.mainloop()
3.创建矩形
#!/usr/bin/python
#coding: utf-8
'''
创建矩形
'''
from Tkinter import *
tk = Tk()
canvas = Canvas(tk, width = 500, height = 500)
canvas.pack()
# 结果返回1,证明画线成功,以后每次都累加
# 参数表示两个点,即矩形的对角线,点(10, 10)和(50, 50)
t = canvas.create_rectangle(10, 10, 50, 50)
print(t)
t = canvas.create_rectangle(10, 10, 300, 50)
print(t)
t = canvas.create_rectangle(10, 10, 50, 300)
print(t)
tk.mainloop()
4.创建多个矩形(使用随机数randrange)
#!/usr/bin/python
#coding: utf-8
'''
创建多个矩形
'''
from Tkinter import *
import random
tk = Tk()
canvas = Canvas(tk, width = 400, height = 400)
canvas.pack()
def random_rectangle(width, height):
# randrange用来产生随机数
# 例如:randrange(100)产生的是(0 ~ 99)的随机数
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 i in range(500):
random_rectangle(250, 250)
tk.mainloop()
#!/usr/bin/python
#coding: utf-8
'''
创建多个矩形
'''
from Tkinter import *
# 导入颜色选择器模块
from tkColorChooser import *
import random
tk = Tk()
canvas = Canvas(tk, width = 400, height = 400)
canvas.pack()
def random_rectangle(width, height, fill_color):
# randrange用来产生随机数
# 例如:randrange(100)产生的是(0 ~ 99)的随机数
x1 = random.randrange(width)
y1 = random.randrange(height)
x2 = x1 + random.randrange(width)
y2 = y1 + random.randrange(height)
# fill填充颜色
canvas.create_rectangle(x1, y1, x2, y2, fill = fill_color)
# 调用颜色选择器模块,当点击OK以后会有一个返回的元组
c = askcolor()
print c
for i in range(10):
random_rectangle(250, 250, c[1])
tk.mainloop()
#!/usr/bin/python
#coding: utf-8
from Tkinter import *
from tkColorChooser import *
tk = Tk()
canvas = Canvas(tk, width = 500, height = 500)
canvas.pack()
# 如果使用的是360度的话,则相当于0度
# 由于此时最大的角度只能是359,所以不能够添加颜色,因为图形没有闭合
# extent表示的是度数
canvas.create_arc(50, 50, 100, 100, extent = 359, style = ARC)
canvas.create_arc(10, 10, 200, 80, extent = 45, style = ARC)
canvas.create_arc(10, 80, 200, 160, extent = 90, style = ARC)
canvas.create_arc(10, 160, 200, 240, extent = 135, style = ARC)
canvas.create_arc(10, 240, 200, 320, extent = 180, style = ARC)
canvas.create_arc(10, 320, 200, 400, extent = 359, style = ARC)
mainloop()
#!/usr/bin/python
#coding: utf-8
'''
画多边形
'''
from Tkinter import *
from tkColorChooser import *
tk = Tk()
canvas = Canvas(tk, width = 500, height = 500)
canvas.pack()
c = askcolor()
# fill 添加颜色参数
# outline 添加边框的颜色
# 表示从(10, 10) 开始,画到(100, 10),然后结束于(100, 110), 下同
canvas.create_polygon(10, 10, 100, 10, 100, 110, fill = c[1], outline = 'black')
del c
c = askcolor()
canvas.create_polygon(200, 10, 240, 30, 120, 100, 140, 120, fill = c[1], outline = 'yellow')
tk.mainloop()
#!/usr/bin/python
#coding: utf-8
'''
显示文字
'''
from Tkinter import *
from tkColorChooser import *
tk = Tk()
canvas = Canvas(tk, width = 500, height = 500)
canvas.pack()
# text为要显示的文字
# font用来指定字体类型和字体大小
canvas.create_text(150, 100, text = "Hello, World", font = ('Time', 15))
canvas.create_text(120, 120, text = "Hello, Python", font = ('Helvetica', 30))
canvas.create_text(220, 300, text = 'Hello, everyone', font = ('Courier', 30))
tk.mainloop()
9.显示图片(注意提示)
#!/usr/bin/python
#coding: utf-8
'''
显示图片
用Tkinter只能装入gif图片,也就是扩展名是.gif的图片文件,想要显示其他类型的图片,就需要用到其他的模块
'''
from Tkinter import *
tk = Tk()
canvas = Canvas(tk, width = 500, height = 500)
canvas.pack()
# 把图片放入变量my_site中
my_image = PhotoImage(file = '/home/wuxiushu/桌面/test10.gif')
# 显示图片
# (0, 0)即为要显示的图片的位置
# anchor = NW 使函数显示在左上角,作为画图的起始点。(NW即为northwest的缩写)
# image = my_image 装入图片
canvas.create_image(0, 0, anchor = NW, image = my_image)
tk.mainloop()
#!/usr/bin/python
#coding: utf-8
'''
图形移动,注意,每创建一个图形,就会返回一个id号,可以通过这个id号来操作对应的图形
'''
from Tkinter import *
import time
tk = Tk()
canvas = Canvas(tk, width = 500, height = 500)
canvas.pack()
# 创建图形的时候会返回一个id,在move中可以用它来指向这个形状
t = canvas.create_polygon(10, 10, 10, 60, 50, 35)
for x in range(60):
# 第一个参数为对应的id的图形
# 后面的两个参数是x和y轴对应的坐标
canvas.move(t, 5, 0)
# 更新图形
tk.update()
time.sleep(0.05)
for x in range(60):
canvas.move(t, -5, 0)
canvas.update()
time.sleep(0.05)
for x in range(60):
canvas.move(t, 5, 5)
canvas.update()
time.sleep(0.05)
tk.mainloop()
#!/usr/bin/python
#coding: utf-8
'''
监听事件,只要子是以create_开头的函数,总会返回一个ID
'''
from Tkinter import *
tk = Tk()
canvas = Canvas(tk, width = 500, height = 500)
canvas.pack()
t = canvas.create_polygon(10, 10, 10, 60, 50, 35)
def movetriangle(event):
# keysym是一个字符串,包含了实际按键的值
if event.keysym == 'Up':
canvas.move(t, 0, -3)
elif event.keysym == 'Down':
canvas.move(t, 0, 3)
elif event.keysym == 'Left':
canvas.move(t, -3, 0)
else:
canvas.move(t, 3, 0)
# 监听事件,当事件发生时调用movetriangle函数
canvas.bind_all('', movetriangle)
canvas.bind_all('', movetriangle)
canvas.bind_all('', movetriangle)
canvas.bind_all('', movetriangle)
tk.mainloop()
#!/usr/bin/python
#coding: utf-8
'''
改变已经创建好的图形的颜色和边框的颜色
'''
from Tkinter import *
from tkColorChooser import *
tk = Tk()
canvas = Canvas(tk, width = 500, height = 500)
canvas.pack()
c = askcolor()
t = canvas.create_polygon(10, 10, 10, 60, 50, 35, fill = c[1], outline = 'red')
del c
c = askcolor()
t = canvas.itemconfig(t, fill = c[1], outline = 'yellow')
tk.mainloop()