Tkinter-Canvas

1.创建

from tkinter import *

root=Tk()

cv=Canvas(root,bg='black')
cv.pack()

root.mainloop()

Tkinter-Canvas_第1张图片
image

2.创建item

from tkinter import *

root=Tk()

cv=Canvas(root,bg='white')
cv.create_rectangle(10,10,110,110)
cv.pack()

root.mainloop()

Tkinter-Canvas_第2张图片
image

3.填充颜色

from tkinter import *

root=Tk()

cv=Canvas(root,bg='white')
cv.create_rectangle(10,10,110,110,fill='red')
cv.pack()

root.mainloop()

Tkinter-Canvas_第3张图片
image

4.边框颜色

from tkinter import *

root=Tk()

cv=Canvas(root,bg='white')
cv.create_rectangle(10,10,110,110,outline='red')
cv.pack()

root.mainloop()

Tkinter-Canvas_第4张图片
image

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

Tkinter-Canvas_第5张图片
image

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

Tkinter-Canvas_第6张图片
image

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

Tkinter-Canvas_第7张图片
image

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

Tkinter-Canvas_第8张图片
image

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

Tkinter-Canvas_第9张图片
image

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

Tkinter-Canvas_第10张图片
image

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

Tkinter-Canvas_第11张图片
image

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

Tkinter-Canvas_第12张图片
image

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

Tkinter-Canvas_第13张图片
image

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

Tkinter-Canvas_第14张图片
image

你可能感兴趣的:(Tkinter-Canvas)