1、tags的说明
Tags 是附在画布对象上的标签,Tags 由普通的非空白字符串组成。一个画布对象可以与多个 Tags 相关联,一个 Tag 也可用于描述多个画布对象。
Canvas 组件预定义了两个 Tags:"all" 和 "current"
"all" 表示 Canvas 组件中的所有画布对象
"current" 表示鼠标指针下的画布对象(如果有的话)
2、addtag_above 为显示列表中 tagOrId 上方的画布对象添加 Tag
from tkinter import * def addtag_above(): """ 为显示列表中 tagOrId 上方的画布对象添加 Tag 该方法相当于 addtag(tag, "above", tagOrId) tagOrId 可以是单个画布对象的 ID,也可以是某个 Tag """ # 参数可以是对象ID canvas.addtag_above(‘newtag‘, line3) # 参数可以是某个tag canvas.addtag_above(‘newtag1‘, ‘line3‘) show() def show(): # 打印画布所有对象的 ID 和 Tags for i in canvas.find_all(): print(f‘id:{i} tags:{canvas.itemcget(i, "tags")}‘) print(‘‘.center(20, ‘=‘)) win = Tk() win.geometry(‘500x500+500+100‘) canvas = Canvas(win, bg=‘white‘) canvas.pack(fill=BOTH, expand=True) line = canvas.create_line(10, 10, 100, 10, width=5, fill=‘pink‘, ) line2 = canvas.create_line(10, 20, 100, 20, width=5, fill=‘pink‘) line3 = canvas.create_line(10, 30, 100, 30, width=5, fill=‘pink‘, tags=‘line3‘) line4 = canvas.create_line(10, 40, 100, 40, width=5, fill=‘pink‘) line5 = canvas.create_line(10, 50, 100, 50, width=5, fill=‘pink‘) btn = Button(text=‘为显示列表中tagOrId上方的画布对象添加Tag‘, command=addtag_above) canvas.create_window((140, 80), window=btn) win.mainloop()
3、addtag_below 为显示列表中 tagOrId 下方的画布对象添加 Tag
from tkinter import * def addtag_below(): """ 为显示列表中 tagOrId 下方的画布对象添加 Tag 该方法相当于 addtag(tag, "below", tagOrId) tagOrId 可以是单个画布对象的 ID,也可以是某个 Tag """ # 参数可以是对象ID canvas.addtag_below(‘newtag‘, line3) # 参数可以是某个tag canvas.addtag_below(‘newtag1‘, ‘line3‘) show() def show(): # 打印画布所有对象的 ID 和 Tags for i in canvas.find_all(): print(f‘id:{i} tags:{canvas.itemcget(i, "tags")}‘) print(‘‘.center(20, ‘=‘)) win = Tk() win.geometry(‘500x500+500+100‘) canvas = Canvas(win, bg=‘white‘) canvas.pack(fill=BOTH, expand=True) line = canvas.create_line(10, 10, 100, 10, width=5, fill=‘pink‘, ) line2 = canvas.create_line(10, 20, 100, 20, width=5, fill=‘pink‘) line3 = canvas.create_line(10, 30, 100, 30, width=5, fill=‘pink‘, tags=‘line3‘) line4 = canvas.create_line(10, 40, 100, 40, width=5, fill=‘pink‘) line5 = canvas.create_line(10, 50, 100, 50, width=5, fill=‘pink‘) btn = Button(text=‘为显示列表中 tagOrId 下方的画布对象添加 Tag‘, command=addtag_below) canvas.create_window((140, 80), window=btn) win.mainloop()
4、addtag_closest 将 Tag 添加到与给定坐标(x, y)相临近的画布对象
from tkinter import * def addtag_closest(): """ 将 Tag 添加到与给定坐标(x, y)相临近的画布对象 可选参数 halo 指定一个距离,表示以(x, y)为中心,halo为半径,最接近半径的画布对象添加 Tag 可选参数 start 指定一个画布对象,该方法将为低于但最接近该对象的画布对象添加 Tag 该方法相当于 addtag(tag, "closet", x, y, halo=None, start=None) 注1:使用的是画布坐标系 注2:如果同时由几个画布对象与给定坐标(x, y)的距离相同,则为位于显示列表上方的那个画布对象添加 Tag """ canvas.addtag_closest(‘newtag‘, 100, 40) canvas.addtag_closest(‘newtag1‘, 10, 28) show() def show(): # 打印画布所有对象的 ID 和 Tags for i in canvas.find_all(): coord = canvas.coords(i) print(f‘id:{i} coord:{coord} tags:{canvas.itemcget(i, "tags")}‘) print(‘‘.center(20, ‘=‘)) win = Tk() win.geometry(‘500x500+500+100‘) canvas = Canvas(win, bg=‘white‘) canvas.pack(fill=BOTH, expand=True) line = canvas.create_line(10, 10, 100, 10, width=5, fill=‘pink‘, ) line2 = canvas.create_line(10, 20, 100, 20, width=5, fill=‘pink‘) line3 = canvas.create_line(10, 30, 100, 30, width=5, fill=‘pink‘) line4 = canvas.create_line(10, 40, 100, 40, width=5, fill=‘pink‘) line5 = canvas.create_line(10, 50, 100, 50, width=5, fill=‘pink‘) btn = Button(text=‘将 Tag 添加到与给定坐标(x, y)相临近的画布对象‘, command=addtag_closest) canvas.create_window((160, 80), window=btn) win.mainloop()
5、addtag_enclosed 为所有坐标在矩形(x1, y1, x2, y2)中的画布对象添加 Tag
from tkinter import * def addtag_enclosed(): """ 为所有坐标在矩形(x1, y1, x2, y2)中的画布对象添加 Tag 该方法相当于 addtag(tag, "enclosed", x1, y1, x2, y2) (画布对象必须全部在矩形中才算) """ canvas.addtag_enclosed(‘newtag‘, 0, 0, 110, 50) show() def show(): # 打印画布所有对象的 ID 和 Tags for i in canvas.find_all(): coord = canvas.coords(i) print(f‘id:{i} coord:{coord} tags:{canvas.itemcget(i, "tags")}‘) print(‘‘.center(20, ‘=‘)) win = Tk() win.geometry(‘500x500+500+100‘) canvas = Canvas(win, bg=‘white‘) canvas.pack(fill=BOTH, expand=True) line = canvas.create_line(10, 10, 100, 10, width=5, fill=‘pink‘, ) line2 = canvas.create_line(10, 20, 100, 20, width=5, fill=‘pink‘) line3 = canvas.create_line(10, 30, 100, 30, width=5, fill=‘pink‘) line4 = canvas.create_line(10, 40, 100, 40, width=5, fill=‘pink‘) line5 = canvas.create_line(10, 50, 100, 50, width=5, fill=‘pink‘) btn = Button(text=‘添加Tag‘, command=addtag_enclosed) canvas.create_window((140, 80), window=btn) win.mainloop()
6、addtag_overlapping 为所有坐标在矩形(x1, y1, x2, y2)中的画布对象添加 Tag
from tkinter import * def addtag_overlapping(): """ addtag_enclosed为所有坐标在矩形(x1, y1, x2, y2)中的画布对象添加 Tag 该方法相当于 addtag(tag, "enclosed", x1, y1, x2, y2) 与addtag_enclosed() 方法相似,不过该方法范围更广(即使画布对象只有一部分在矩形中也算) 该方法相当于 addtag(tag, "overlapping", x1, y1, x2, y2) """ canvas.addtag_overlapping(‘newtag‘, 0, 0, 110, 50) show() def show(): # 打印画布所有对象的 ID 和 Tags for i in canvas.find_all(): coord = canvas.coords(i) print(f‘id:{i} coord:{coord} tags:{canvas.itemcget(i, "tags")}‘) print(‘‘.center(20, ‘=‘)) win = Tk() win.geometry(‘500x500+500+100‘) canvas = Canvas(win, bg=‘white‘) canvas.pack(fill=BOTH, expand=True) line = canvas.create_line(10, 10, 100, 10, width=5, fill=‘pink‘, ) line2 = canvas.create_line(10, 20, 100, 20, width=5, fill=‘pink‘) line3 = canvas.create_line(10, 30, 100, 30, width=5, fill=‘pink‘) line4 = canvas.create_line(10, 40, 100, 40, width=5, fill=‘pink‘) line5 = canvas.create_line(10, 50, 100, 50, width=5, fill=‘pink‘) btn = Button(text=‘添加Tag‘, command=addtag_overlapping) canvas.create_window((140, 80), window=btn) win.mainloop()
7、addtag_withtag 为 tagOrId 参数指定的画布对象添加 Tag
from tkinter import * def addtag_withtag(): """ 为 tagOrId 参数指定的画布对象添加 Tag tagOrId 参数如果指定一个 Tag,则为所有拥有此 Tag 的画布对象添加新的 Tag tagOrId 参数如果指定一个画布对象,那么只为其添加 Tag 该方法相当于 addtag(tag, "withtag", tagOrId) tagOrId 可以是单个画布对象的 ID,也可以是某个 Tag """ # 参数可以是对象ID canvas.addtag_withtag(‘new_tag‘, line) # 参数可以是某个tag canvas.addtag_withtag(‘new_tag1‘, ‘line‘) show() def show(): # 打印画布所有对象的 ID 和 Tags for i in canvas.find_all(): coord = canvas.coords(i) print(f‘id:{i} coord:{coord} tags:{canvas.itemcget(i, "tags")}‘) print(‘‘.center(20, ‘=‘)) win = Tk() win.geometry(‘500x500+500+100‘) canvas = Canvas(win, bg=‘white‘) canvas.pack(fill=BOTH, expand=True) line = canvas.create_line(10, 10, 100, 10, width=5, fill=‘pink‘) line2 = canvas.create_line(10, 20, 100, 20, width=5, fill=‘pink‘) line3 = canvas.create_line(10, 30, 100, 30, width=5, fill=‘pink‘, tags=‘line‘) line4 = canvas.create_line(10, 40, 100, 40, width=5, fill=‘pink‘, tags=‘line‘) line5 = canvas.create_line(10, 50, 100, 50, width=5, fill=‘pink‘) btn = Button(text=‘添加Tag‘, command=addtag_withtag) canvas.create_window((140, 80), window=btn) win.mainloop()
8、addtag_all 为画布所有对象添加 Tag
from tkinter import * def addtag_all(): """ 为画布所有对象添加 Tag 该方法相当于 addtag(tag, "all") """ canvas.addtag_all(‘new_tag‘) show() def show(): # 打印画布所有对象的 ID 和 Tags for i in canvas.find_all(): coord = canvas.coords(i) print(f‘id:{i} coord:{coord} tags:{canvas.itemcget(i, "tags")}‘) print(‘‘.center(20, ‘=‘)) win = Tk() win.geometry(‘500x500+500+100‘) canvas = Canvas(win, bg=‘white‘) canvas.pack(fill=BOTH, expand=True) line = canvas.create_line(10, 10, 100, 10, width=5, fill=‘pink‘) line2 = canvas.create_line(10, 20, 100, 20, width=5, fill=‘pink‘) line3 = canvas.create_line(10, 30, 100, 30, width=5, fill=‘pink‘) line4 = canvas.create_line(10, 40, 100, 40, width=5, fill=‘pink‘) line5 = canvas.create_line(10, 50, 100, 50, width=5, fill=‘pink‘) btn = Button(text=‘添加Tag‘, command=addtag_all) canvas.create_window((140, 80), window=btn) win.mainloop()
原文:https://www.cnblogs.com/rainbow-tan/p/14852553.html