import tkinter as tk
root = tk.Tk()
root.geometry()
cv = tk.Canvas(root, bg = 'gray')
cv.pack()
rt1 = cv.create_rectangle(50,50,110,110,
activefill = 'gray75',tag = ('r','r1'))
rt2 = cv.create_rectangle(150,50,200,110,width = 3,
activefill = 'gray75',tag = ('r','r2'))
rt2 = cv.create_rectangle(50,150,100,200,width = 5,
activefill = 'gray75',tag = ('r','r3'))
生成三个矩形,当鼠标在矩形内部时,会填充颜色,此处为tkinter内置bitmap的’gray75‘。
import tkinter as tk
root = tk.Tk()
root.geometry()
cv = tk.Canvas(root, bg = 'gray')
cv.pack()
rt1 = cv.create_rectangle(50,50,110,110,
activefill = 'gray75',tag = ('r','r1'))
rt2 = cv.create_rectangle(150,50,200,110,width = 3,
activefill = 'gray75',tag = ('r','r2'))
rt3 = cv.create_rectangle(50,150,100,200,width = 5,
activefill = 'gray75',tag = ('r','r3'))
def leftClick(event):
//单击左键时,打印 left click
print('left click')
def rightClick(event):
//单击右键时,打印 right click
print('right click')
cv.tag_bind('r','' ,leftClick) //绑定左键事件
cv.tag_bind('r','' ,rightClick) //绑定右键事件
这里,事件与tag “r”绑定,所以对每一个矩形进行单击左键或右键的操作都可以print结果。也可以与单个对象的tag绑定(“r1”,“r2”,“r3”),只有单个对象会响应对应的事件。
import tkinter as tk
root = tk.Tk()
root.geometry()
cv = tk.Canvas(root, bg = 'gray')
cv.pack()
rt1 = cv.create_rectangle(50,50,110,110,
activefill = 'gray75',tag = ('r','r1'))
rt2 = cv.create_rectangle(150,50,200,110,width = 3,
activefill = 'gray75',tag = ('r','r2'))
rt3 = cv.create_rectangle(50,150,100,200,width = 5,
activefill = 'gray75',tag = ('r','r3'))
print(cv.coords('r1')) //返回对象的坐标,type: list
[50.0, 50.0, 110.0, 110.0]
import tkinter as tk
root = tk.Tk()
root.geometry()
cv = tk.Canvas(root, bg = 'gray')
cv.pack()
rt1 = cv.create_rectangle(50,50,110,110,
activefill = 'gray75',tag = ('r','r1'))
rt2 = cv.create_rectangle(150,50,200,110,width = 3,
activefill = 'gray75',tag = ('r','r2'))
rt3 = cv.create_rectangle(50,150,100,200,width = 5,
activefill = 'gray75',tag = ('r','r3'))
cv.scale('r1',0,0,2,1) //Scale item (XORIGIN, YORIGIN, XSCALE, YSCALE)
print(cv.coords('r1'))
需要传入4个参数 (XORIGIN, YORIGIN, XSCALE, YSCALE),前两个参数为缩放中心,后两个参数为x方向和y方向的缩放倍率。(0,0,2,1)代表,以画布的(0,0)为中心,x坐标变为之前的2倍,y坐标不变。
[50.0, 50.0, 110.0, 110.0] // scale前
[100.0, 50.0, 220.0, 110.0] // scale后,x坐标变为之前的2倍
import tkinter as tk
root = tk.Tk()
root.geometry()
cv = tk.Canvas(root, bg = 'gray')
cv.pack()
rt1 = cv.create_rectangle(50,50,110,110,
activefill = 'gray75',tag = ('r','r1')) #设置属性
rt2 = cv.create_rectangle(50,50,110,110,width = 3,
activefill = 'gray75',tag = ('r','r2')) #设置属性
cv.scale('r1',50,50,2,2) //以矩形左上角的点为缩放中心
print(cv.coords('r1'))
print(cv.coords('r2'))
以矩形左上角的点为缩放中心,x和y都扩大2倍,可以将矩形的面积变为之前4倍。
[50.0, 50.0, 170.0, 170.0] // scale后
[50.0, 50.0, 110.0, 110.0] // scale前
import tkinter as tk
def dele(event):
r_idx = cv.find_closest(event.x, event.y) #找出离x,y最近的对象
print('cv.gettags(r_idx):', cv.gettags(r_idx)[1])
cv.delete('current') #删除当前active对象
root = tk.Tk()
root.geometry()
cv = tk.Canvas(root, bg = 'gray')
cv.pack()
rt1 = cv.create_rectangle(50,50,110,110,
activefill = 'gray75',tag = ('r','r1')) #设置属性
rt2 = cv.create_rectangle(50,50,110,110,width = 3,
activefill = 'gray75',tag = ('r','r2')) #设置属性
cv.tag_bind('r','' ,dele)