1.创建
from tkinter import *
root=Tk()
cv=Canvas(root,bg='black')
cv.pack()
root.mainloop()
2.创建item
from tkinter import *
root=Tk()
cv=Canvas(root,bg='white')
cv.create_rectangle(10,10,110,110)
cv.pack()
root.mainloop()
3.填充颜色
from tkinter import *
root=Tk()
cv=Canvas(root,bg='white')
cv.create_rectangle(10,10,110,110,fill='red')
cv.pack()
root.mainloop()
4.边框颜色
from tkinter import *
root=Tk()
cv=Canvas(root,bg='white')
cv.create_rectangle(10,10,110,110,outline='red')
cv.pack()
root.mainloop()
5.边框宽度
from tkinter import *
root=Tk()
cv=Canvas(root,bg='white')
cv.create_rectangle(10,10,110,110,outline='red',width=5)
cv.pack()
root.mainloop()
6.画虚线
from tkinter import *
root=Tk()
cv=Canvas(root,bg='white')
cv.create_rectangle(10,10,110,110,outline='red',dash=10)
cv.pack()
root.mainloop()
7.创建tags
from tkinter import *
root=Tk()
cv=Canvas(root,bg='white')
rt=cv.create_rectangle(10,10,110,110,tags='r1')
cv.pack()
print(cv.gettags(rt))
cv.itemconfig(rt,tags=('r2','r3','r4'))
print(cv.gettags(rt))
root.mainloop()
8.使用相同tag
from tkinter import *
root=Tk()
cv=Canvas(root,bg='white')
rt=cv.create_rectangle(10,10,110,110,tags=('r1','r2','r3'))
cv.pack()
print(cv.gettags(rt))
cv.create_rectangle(20,20,80,80,tags='r3')
print(cv.find_withtag('r3'))
root.mainloop()
9.通过tag访问item
from tkinter import *
root=Tk()
cv=Canvas(root,bg='white')
rt=cv.create_rectangle(10,10,110,110,tags=('r1','r2','r3'))
cv.pack()
cv.create_rectangle(20,20,80,80,tags='r3')
for item in cv.find_withtag('r3'):
cv.itemconfig(item,outline='blue')
root.mainloop()
10.向item添加tag
from tkinter import *
root=Tk()
cv=Canvas(root,bg='white')
rt1=cv.create_rectangle(10,10,110,110,tags=('r1','r2','r3'))
rt2=cv.create_rectangle(20,20,80,80,tags=('s1','s2','s3'))
rt3=cv.create_rectangle(30,30,70,70,tags=('y1','y2','y3'))
cv.addtag_above('r4',rt2)
cv.addtag_below('r5',rt2)
for item in [rt1,rt2,rt3]:
print(cv.gettags(item))
cv.pack()
root.mainloop()
11.返回其他item
from tkinter import *
root=Tk()
cv=Canvas(root,bg='white')
rt1=cv.create_rectangle(10,10,110,110,tags=('r1','r2','r3'))
rt2=cv.create_rectangle(20,20,80,80,tags=('s1','s2','s3'))
rt3=cv.create_rectangle(30,30,70,70,tags=('y1','y2','y3'))
cv.itemconfig(cv.find_above(rt2),outline='red')
cv.itemconfig(cv.find_below(rt2),outline='green')
cv.pack()
root.mainloop()
12.改变item在stack的顺序
from tkinter import *
root=Tk()
cv=Canvas(root,bg='white')
rt1=cv.create_rectangle(10,10,110,110,tags=('r1','r2','r3'))
rt2=cv.create_rectangle(20,20,80,80,tags=('s1','s2','s3'))
rt3=cv.create_rectangle(30,30,70,70,tags=('y1','y2','y3'))
cv.tag_lower(rt3)
cv.tag_raise(rt1)
cv.itemconfig(cv.find_above(rt2),outline='red')
cv.itemconfig(cv.find_below(rt2),outline='green')
cv.pack()
root.mainloop()
13.移动item
from tkinter import *
root=Tk()
cv=Canvas(root,bg='white')
rt1=cv.create_rectangle(10,10,110,110,tags=('r1','r2','r3'))
cv.create_rectangle(10,10,110,110,tags=('r1','r2','r3'))
cv.move(rt1,20,-10)
cv.pack()
root.mainloop()
14.删除item
from tkinter import *
root=Tk()
cv=Canvas(root,bg='white')
rt1=cv.create_rectangle(10,10,110,110,tags=('r1','r2','r3'))
rt2=cv.create_rectangle(20,20,80,80,tags=('s1','s2','s3'))
cv.delete(rt1)
cv.delete('s1')
cv.pack()
root.mainloop()