# 【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，画一条直线

>>> 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 *


((67.26171875, 170.6640625, 188.734375), '#43aabc')

>>> 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))



## 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)


## 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)


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

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编程》