tkinter绘图之Canvas的使用

t k i n t e r 绘图之 C a n v a s 的使用 tkinter绘图之Canvas的使用 tkinter绘图之Canvas的使用

一 创建画布

from tkinter import *
win= Tk()
win.title("创建画布")
win.geometry("600x400")
canvas= Canvas(win,width=200,height=200,bg="#AAAAAA").pack()
win.mainloop()

tkinter绘图之Canvas的使用_第1张图片

二 绘制线条

from tkinter import *
win= Tk()
win.title("创建画布")
win.geometry("600x400")
canvas= Canvas(win,width=600,height=400,bg="#AAAAAA")
line1 = canvas.create_line(*(14,65,66,65),fill="red")
canvas.pack()

win.mainloop()

tkinter绘图之Canvas的使用_第2张图片

三 绘制矩形

from tkinter import *
win= Tk()
win.title("创建画布")
win.geometry("600x400")
canvas= Canvas(win,width=600,height=400,bg="#AAAAAA")
line1 = canvas.create_rectangle(*(14,65,160,160),fill="red")
canvas.pack()

win.mainloop()

tkinter绘图之Canvas的使用_第3张图片

四 绘制椭圆‘

from tkinter import *
win= Tk()
win.title("创建画布")
win.geometry("600x400")
canvas= Canvas(win,width=600,height=400,bg="#AAAAAA")
line1 = canvas.create_oval(*(14,65,160,160),fill="red")
canvas.pack()

win.mainloop()

tkinter绘图之Canvas的使用_第4张图片

五 绘制圆弧

from tkinter import *
win= Tk()
win.title("创建画布")
win.geometry("600x400")
canvas= Canvas(win,width=600,height=400,bg="#AAAAAA")
line1 = canvas.create_arc(*(14,65,160,160),fill="red")
canvas.pack()

win.mainloop()

tkinter绘图之Canvas的使用_第5张图片

六 绘制多边形

from tkinter import *
win= Tk()
win.title("创建画布")
win.geometry("600x400")
canvas= Canvas(win,width=600,height=400,bg="#AAAAAA")
line1 = canvas.create_polygon(*(14,65,160,160,200,300),fill="red")
canvas.pack()

win.mainloop()

tkinter绘图之Canvas的使用_第6张图片

六 绘制文字

from tkinter import *
from PIL import Image, ImageTk

win= Tk()
win.title("创建画布")


win.geometry("600x400")
canvas= Canvas(win,width=600,height=400,bg="#AAAAAA")
line1 = canvas.create_text(200,200,text="hello world!",fill="red")
canvas.pack()

win.mainloop()

tkinter绘图之Canvas的使用_第7张图片

七 绘制图像

import tkinter
tk = tkinter.Tk()
c = tkinter.Canvas()
image = tkinter.PhotoImage(file='test.png')
c.create_image((0, 0), image=image)
c.pack()
tk.mainloop()

tkinter绘图之Canvas的使用_第8张图片

八 拖动图像

# move
canvas.move(rect,10,10)
from tkinter import *
from PIL import Image, ImageTk

def draw(event):
    canvas.coords(rect,event.x,event.y,event.x+90,event.y+90)

def delete():
    canvas.delete("all")


win = Tk()
win.title("创建画布")
win.geometry("600x400")
canvas = Canvas(win, width=600, height=400, bg="#AAAAAA")

rect = canvas.create_rectangle(10,10,100,100,fill="blue")
canvas.grid(row=0,column=0,columnspan=2)
canvas.bind("",draw)
win.mainloop()

tkinter绘图之Canvas的使用_第9张图片

九 用鼠标绘制图形

tkinter绘图之Canvas的使用_第10张图片

from tkinter import *
from PIL import Image, ImageTk

def draw(event):
    global text1
    text1 = canvas.create_oval(event.x,event.y,event.x+10,event.y+10,fill="green",outline="")

def delete():
    canvas.delete("all")
    generate()

win = Tk()
win.title("创建画布")
win.geometry("600x400")
canvas = Canvas(win, width=600, height=400, bg="#AAAAAA")

def generate():
    canvas.pack()
    canvas.bind("",draw)

Button(win,text="clear",command=delete).pack(side="bottom")
generate()
win.mainloop()

十 设计动画

你可能感兴趣的:(前端,javascript,python)