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()
<figcaption style="display: block; margin-top: 0.5em; margin-bottom: 1em; color: rgb(85, 93, 102); text-align: center; font-size: 13px;">
</figcaption>
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()
15.缩放item
from tkinter import *
root=Tk()
cv=Canvas(root,bg='white')
rt1=cv.create_rectangle(10,10,110,110,tags=('r1','r2','r3'))
cv.scale(rt1,0,0,1,2)
cv.pack()
root.mainloop()
16.事件绑定
from tkinter import *
root=Tk()
cv=Canvas(root,bg='white')
rt1=cv.create_rectangle(10,10,110,110,tags=('r1','r2','r3'))
def printRect(event):
print('rectangle')
cv.tag_bind('r1','<Button-1>',printRect)
cv.pack()
root.mainloop()
点击边框触发事件
17.弧形
from tkinter import *
root=Tk()
cv=Canvas(root,bg='white')
cv.create_arc((10,10,110,110))
cv.pack()
root.mainloop()
18.弧形样式
from tkinter import *
root=Tk()
cv=Canvas(root,bg='white')
d={1:PIESLICE,2:CHORD,3:ARC}
for i in d:
cv.create_arc((10,10+60*i,110,110+60*i),style=d[i])
print(i,d[i])
cv.pack()
root.mainloop()
19.弧形角度
from tkinter import *
root=Tk()
cv=Canvas(root,bg='white')
d={1:PIESLICE,2:CHORD,3:ARC}
for i in d:
cv.create_arc((10,10+60*i,110,110+60*i),style=d[i],start=30,extent=30)
print(i,d[i])
cv.pack()
root.mainloop()
20.绘制位图
from tkinter import *
root=Tk()
cv=Canvas(root,bg='white')
d={1:'error',2:'info',3:'question',4:'hourglass'}
for i in d:
cv.create_bitmap((20*i,20*i),bitmap=d[i])
cv.pack()
root.mainloop()
21.直线
from tkinter import *
root=Tk()
cv=Canvas(root,bg='white')
d=[(0,'none'),(1,'first'),(2,'last'),(3,'both')]
for i in d:
cv.create_line((10,10+i[0]*20,110,110+i[0]*20),arrow=i[1],arrowshape='40 40 10')
cv.pack()
root.mainloop()
22.直线的joinstyle属性
from tkinter import *
root=Tk()
cv=Canvas(root,bg='white')
d=[(0,'none','bevel'),(1,'first','miter'),(2,'last','round'),(3,'both','round')]
for i in d:
cv.create_line((10,10+i[0]*20,110,110+i[0]*20),arrow=i[1],arrowshape='8 10 3',joinstyle=i[2])
cv.pack()
root.mainloop()
23.椭圆
from tkinter import *
root=Tk()
cv=Canvas(root,bg='white')
cv.create_oval((10,10,210,110),fill='red')
cv.pack()
root.mainloop()
24.多边形
from tkinter import *
root=Tk()
cv=Canvas(root,bg='white')
cv.create_polygon((10,10,10,200,100,200),fill='red')
cv.pack()
root.mainloop()
25.文本
from tkinter import *
root=Tk()
cv=Canvas(root,bg='white')
cv.create_text((10,10),text='Hello Text',anchor=W)
cv.pack()
root.mainloop()
26.选中文本
from tkinter import *
root=Tk()
cv=Canvas(root,bg='white')
txt=cv.create_text((10,10),text='Hello Text',anchor=W)
cv.select_from(txt,2)
cv.select_to(txt,6)
cv.pack()
root.mainloop()
27.创建组件
from tkinter import *
root=Tk()
cv=Canvas(root,bg='white')
def printWindow():
print('window')
bt=Button(cv,text='ClickMe',command=printWindow)
cv.create_window((10,10),window=bt,anchor=W)
cv.create_line(10,10,20,20)
cv.create_line(30,30,100,100)
cv.pack()
root.mainloop()