【7】图形相关-4-用tkinter做高级图
1,创建一个可以点的按钮
>>> def hello():
print('hello there')
>>> from tkinter import *
>>> tk=Tk()
>>> btn=Button(tk,text="click me",command=hello)
>>> btn.pack()
2,创建一个画布
>>> from tkinter import *
>>> tk=Tk()
>>> canvas=Canvas(tk,width=500,height=500)
>>> canvas.pack()
3,画一条直线
像素为500*500时,右下角为(500,500)
>>> from tkinter import *
>>> tk=Tk()
>>> canvas=Canvas(tk,width=500,height=500)
>>> canvas.pack()
>>> canvas.create_line(0,0,500,500)
4,用turtle来做上面的图
>>> import turtle
>>> turtle.setup(width=500,height=500)
>>> t=t=turtle.Pen()
>>> t.up()
>>> t.goto(-250,-50)
>>> t.down()
>>> t.goto(500,-500)
5,画矩形
>>> from tkinter import *
>>> tk=Tk()
>>> canvas=Canvas(tk,width=400,height=400)
>>> canvas.pack()
>>> canvas.create_rectangle(10,10,50,50)
#(10,10)代表这个方形的左上角,(50,50)代表方形的右下角
画许多矩形
>>> 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)
6,设置颜色
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=x1+random.randrange(width)
y2=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,'#ffd800')
#ffd800表示十六进制
>>> from tkinter import *
>>> colorchooser.askcolor()
选择一个颜色,按确定,就会出现颜色的的编号
((67.26171875, 170.6640625, 188.734375), '#43aabc')
>>> c=colorchooser.askcolor()
>>> random_rectangle(400,400,c[1])
也可以选择一种颜色,保存到一个变量里面
7,画弧形
>>> 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)
extent代表弧度
8,画多边形
>>> 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")
每两个数字构成一个点的坐标,然后依次画过去
>>> canvas.create_polygon(200,10,240,30,120,100,140,120,fill="",outline="black")
9,显示文字
>>> 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')
>>> canvas.create_text(130,120,text='there once was a man from Toulouse',fill='red')
>>> canvas.create_text(150,150,text='there once was a man from Toulouse',fill='red',font=('Times',25))
注:font后字体以及大小
10,显示图片
>>> from tkinter import *
>>> tk=Tk()
>>> canvas=Canvas(tk,width=400,height=400)
>>> canvas.pack()
>>> my_image=PhotoImage(file='E:\\py\\111.gif')
>>> canvas.create_image(0,0,anchor=NW,image=my_image)
注:tkinter 只能装入gif图片,anchor=NW,NW为左上角northwest,作为图片的起点
11,创建基本的动画
import time
from tkinter import *
tk=Tk()
canvas=Canvas(tk,width=400,height=200)
canvas.pack()
canvas.create_polygon(10,10,10,60,50,35)
for x in range(0,60):
canvas.move(1,5,0)
tk.update()
time.sleep(0.05)
注:canvas.move(1,5,0) 其中1代表代表打印出来的那个多边形,5,0分别代表横纵坐标,tk.update()代表重新作图,
用enter键来控制图形的移动
from tkinter import *
tk=Tk()
canvas=Canvas(tk,width=400,height=400)
canvas.pack()
canvas.create_polygon(10,10,10,60,50,35)
def movetriangle(event):
canvas.move(1,5,0)
canvas.bind_all('< KeyPress-Return>',movetriangle)
使用上下左右来调动图形
from tkinter import *
tk=Tk()
canvas=Canvas(tk,width=400,height=400)
canvas.pack()
canvas.create_polygon(10,10,10,60,50,35)
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)
canvas.bind_all('< KeyPress-Down>',movetriangle)
canvas.bind_all('< KeyPress-Left>',movetriangle)
canvas.bind_all('< KeyPress-Right>',movetriangle)
12,多个ID
只要用了以create开头的函数,例如create_polygon或者create_ractangle等等,它总会返回一个ID,这个识别编号可以在其他画布的函数中使用,同时我们也可以把图形保存在一个变量里面
from tkinter import *
tk=Tk()
canvas=Canvas(tk,width=400,height=400)
canvas.pack()
mytriangle=canvas.create_polygon(10,10,10,60,50,35)
canvas.move(mytriangle,5,0)
canvas.itemconfig(mytriangle,fill='blue')
canvas.itemconfig(mytriangle,outline='red')
参考资料:
《趣学python编程》
这里是一个广告位,,感兴趣的都可以发邮件聊聊:tiehan@sina.cn
个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn
个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn