Tkinter (03) 畫布部件 Canvas
画布部件的创建及其选项
import tkinter as tk
parent = tk.Tk()
w = tk.Canvas(parent, option=value, ...)
bd or borderwidth | 边框宽度, 默认值 2 点 |
bg or background | 背景色, 默认值 '#E4E4E4' 淡灰色 |
closeenough | 该距离以内都算该部件的范围之内, float, 默认值 1.0 |
confine | 画布无法在scrollregion之外滚动, 默认为 True |
cursor | 鼠标悬停在画布上时显示的光标 |
height | 画布的 Y 尺寸 |
highlightbackground | 非聚焦时高亮颜色 |
highlightcolor | 聚焦时高亮颜色 |
highlightthickness | 高亮厚度, 默认为 1 |
relief | 画布的浮雕样式,默认为 tk.FLAT/td> |
scrollregion | 画布可以滚动的区域, tuple (left, top, right, bottom) |
selectbackground | 所选项目的背景色 |
selectborderwidth | 所选项目的边框宽度 |
selectforeground | 所选项目的前景色 |
takefocus | TAB 键在画布是否会循环焦点, 1 則會, 为空字符串, 则仅在具有键绑定下循环焦点. |
width | 画布的X尺寸 |
xscrollincrement | X 维度上的滚动单位,设置为 0, 则可以滚动到的任何位置 |
xscrollcommand | 水平滚动条 scrollbar.set() 方法 |
yscrollincrement | Y 维度上的滚动单位,设置为 0, 则可以滚动到的任何位置 |
yscrollcommand | 垂直滚动条 scrollbar.set() 方法 |
画布的一些特性
- Coordinate 座标 - 左上角为 (0, 0), 右为正的 X 轴, 下为正的 Y 轴.
- Display list 显示列表 - 顺序为从背景到前景, 新项目加到列表的顶端.
- Object ID 项目识别码 - 唯一的整数
- Tags 标签名 - string, 可以代表没有或有一个以上的项目, 一个项目可以没有或有一个以上的标签. 标签用来代表其所有的项目, 可以用来一次设定其相闗的动作.
- tagOrId 标签名或项目识别码
- integer 代表一个项目的识别码
- string, 代表相关项目的标签名
画布中项目
import tkinter as tk
canvas = Tk.Canvas(...)
- Arc 弧形
(x0, y0) 为区域的左上角, (x1, y1) 为区域的右下角, 弧就以该区域作为其范围.
id_ = canvas.create_arc(x0, y0, x1, y1, option, ...)
- Bitmap 点阵
(x, y) 为点阵图的参考位置座标, 参考的定位由选项 anchor 而定.
id_ = canvas.create_bitmap(x, y, *options)
- Image 图片
(x, y) 为图片的参考位置座标, 参考的定位由选项 anchor 而定.
id_ = canvas.create_image(x, y, *options)
- Line 线段
(x0, y0), (x1, y1), …, (xn, yn) 为各线段中的点, 两点间为直线.
id_ = canvas.create_line(x0, y0, x1, y1, ..., xn, yn, option, ...)
- Oval 椭圆
(x0, y0) 为区域的左上角, (x1, y1) 为区域的右下角(不含), 弧就以该区域作为其范围.
id_ = canvas.create_oval(x0, y0, x1, y1, option, ...)
- Polygon 多边形
(x0, y0), (x1, y1), … 为多边形的各端点.
id_ = canvas.create_polygon(x0, y0, x1, y1, ..., option, ...)
- Rectangle 矩形
(x0, y0), (x1, y1) 外框与内部含 (x0, y0), 外框含 (x1, y1), 内部不含 (x1, y1), 外框为主.
id_ = canvas.create_rectangle(x0, y0, x1, y1, option, ...)
- Text 文本
以 (x, y) 作为参考点, 建立一个文本物件, 返回物件的 ID.
id_ = canvas.create_text(x, y, option)
选项 & 说明 | 弧形 | 点阵 | 图片 | 线段 | 椭圆 | 多边 | 矩形 | 本文 | 视窗 |
---|---|---|---|---|---|---|---|---|---|
activebackground active时, 颜色 0 的颜色, 内定空字符串(透明) |
✔ | ||||||||
activebitmap active 点阵图 |
✔ | ||||||||
activedash active 点线样式 |
✔ | ✔ | ✔ | ✔ | ✔ | ✔ | |||
activefill active 区域的填色 |
✔ | ✔ | ✔ | ✔ | ✔ | ✔ | |||
activeforeground active 颜色 1 的颜色, 内定空字符串(黑色) |
✔ | ||||||||
activeimage active 图片 |
✔ | ||||||||
activeoutline active 线颜色, 内定空字符串(黑色) |
✔ | ✔ | ✔ | ✔ | |||||
activeoutlinestipple active 线的点阵图, 内定空字符串(黑色) |
✔ | ✔ | ✔ | ✔ | |||||
activestipple active 区域的点阵图, 内定空字符串(黑色) |
✔ | ✔ | ✔ | ✔ | ✔ | ✔ | |||
activewidth active 线的寛度, 内定为 1 点素. |
✔ | ✔ | ✔ | ✔ | ✔ | ||||
anchor 定位方式, 内定为CENTER |
✔ | ✔ | ✔ | ✔ | |||||
arrow 线端的箭头, 内定无/FIRST/LAST/BOTH |
✔ | ||||||||
arrowshape 箭头图形的三边长 (d1, d2, d3) |
✔ | ||||||||
background 颜色值为 0 的颜色, 内定空字符串(透明) |
✔ | ||||||||
bitmap 所使用的点阵图 |
✔ | ||||||||
capstyle 线端的样式, 内定为 BUTT. |
✔ | ||||||||
dash 点线样式 |
✔ | ✔ | ✔ | ✔ | ✔ | ||||
dashoffset 点线起始间隔 |
✔ | ✔ | ✔ | ✔ | ✔ | ||||
disabledbackground disabled 颜色值为 0 的颜色, 内定空字符串(透明) |
✔ | ||||||||
disabledbitmap disabled 点阵图. |
✔ | ||||||||
disableddash disabled 点线样式 |
✔ | ✔ | ✔ | ✔ | ✔ | ||||
disabledfill disabled 区域的填色, 内定空字符串(透明) |
✔ | ✔ | ✔ | ✔ | ✔ | ✔ | |||
disabledforeground disabled 颜色值为 1 的颜色, 内定空字符串(黑色) |
✔ | ||||||||
disabledimage disabled 图片 |
✔ | ||||||||
disabledoutline disabled 线颜色, 内定空字符串(黑色) |
✔ | ✔ | ✔ | ✔ | |||||
disabledoutlinestipple disabled 线的点阵图, 内定空字符串(黑色) |
✔ | ✔ | ✔ | ✔ | |||||
disabledstipple disabled 区域的点阵图, 内定空字符串(黑色) |
✔ | ✔ | ✔ | ✔ | ✔ | ✔ | |||
disabledwidth disabled 线寛, 内定为 1 点素. |
✔ | ✔ | ✔ | ✔ | ✔ | ||||
extent 逆时针所涵盖的角度 |
✔ | ||||||||
fill 区域的填色, 内定空字符串(透明) |
✔ | ✔ | ✔ | ✔ | ✔ | ✔ | |||
font 文本字体 |
✔ | ||||||||
foreground 颜色值为 1 的颜色, 内定空字符串(黑色) |
✔ | ||||||||
height 区域的高度 |
✔ | ||||||||
image 使用的图片. |
✔ | ||||||||
joinstyle 线转角的样式, 内定为 ROUND |
✔ | ✔ | |||||||
justify 多行文本的对方式, 内定为 LEFT |
✔ | ||||||||
offset 区域对齐的调整偏差 |
✔ | ✔ | ✔ | ✔ | ✔ | ✔ | |||
outline 线颜色, 内定空字符串(黑色) |
✔ | ✔ | ✔ | ✔ | |||||
outlineoffset 线对齐的调整偏差 |
✔ | ✔ | ✔ | ✔ | |||||
outlinestipple 线的点阵图, 内定空字符串(黑色) |
✔ | ✔ | ✔ | ✔ | |||||
smooth 两点间的平滑方式, 内定False直线/True抛物线 |
✔ | ✔ | |||||||
splinesteps smooth=True, 每一线段以多少个子线段来组成. |
✔ | ✔ | |||||||
start 线的起始角度, x正轴开始, 未设置为整个椭圆形 |
✔ | ||||||||
state 状态NORMAL内定/HIDDEN/DISABLED |
✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ |
stipple 区域的点阵图, 内定空字符串(黑色) |
✔ | ✔ | ✔ | ✔ | ✔ | ✔ | |||
style 样式, PIESLICE扇形/CHORD月形/ARC弧形 |
✔ | ||||||||
tags 设置标签, 单一标签 str 或多标签的 tuple. |
✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ |
text 文本字串, 使用 ‘\n’ 分行 |
✔ | ||||||||
width 寛度, 内定为 1 点素. / 文本为字寛 |
✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ||
window 要置入的部件 |
✔ |
画布部件有关项目的方法
方法 & 說明 |
---|
addtag_above(newTag, tagOrId) 在紧接 tagOrId 上方的一个项目添加新标签 |
addtag_all(newTag) 向 Canvas 上的所有项目添加新标签 |
addtag_below(newTag, tagOrId) 在紧接 tagOrId 下方的一个项目添加新标签 |
addtag_closest(newTag, x, y, halo=None, start=None) 将newTag 添加到最靠近屏幕坐标(x, y) 的项目. 如果有多个项目, 则选择较高的一个. halo 代表涵盖的距离范围, 项目限定在start 项目之下. |
addtag_enclosed(newTag, x1, y1, x2, y2) 对区域 (x1, y1), (x2, y2) 中完全涵盖的所有项目添加 newTagAdd |
addtag_overlapping(newTag, x1, y1, x2, y2) 对区域 (x1, y1), (x2, y2) 中有交集的所有项目添加 newTagAdd |
addtag_withtag(newTag, tagOrId) 向 tagOrId 指定的所有项目添加 newTagAdd |
bbox(tagOrId=None) 取得 tagOrId 所有项目涵盖的区域(x1, y1), (x2, y2), 如果没有设置tagOrId , 则为画布中所有的项目. |
canvasx(screenx, gridspacing=None) 将窗口坐标 screenx 转换为画布坐标,并为最近的网格间距 gridspacing 的整数倍. |
canvasy(screeny, gridspacing=None) 将窗口坐标 screeny 转换为画布坐标,并为最近的网格间距 gridspacing 的整数倍. |
coords(tagOrId, x0, y0, x1, y1, …, xn, yn) 返回 tagOrId 最下层项目的涵盖范围, 或/且移动项目到新的座标处 (x0, y0, x1, y1, .., xn, yn) |
dchars(tagOrId, first=0, last=first) 从 first 到last (含) 处, 删除本文中的字符串, 索引first 及last 为integer, 或end 代表最尾端处 |
delete(tagOrId) 删除 tagOrId 中的项目 |
dtag(tagOrId, tagToDelete) 从 tagOrId 中删除 tagToDelete 的项目 |
find_above(tagOrId) 返回紧接 tagOrId 上方的项目 ID , 无则返回空 tuple |
find_all() 返回画布中所有的项目 |
find_below(tagOrId) 返回紧接 tagOrId 下方的项目 ID , 无则返回空 tuple |
find_closest(x, y, halo=None, start=None) 返回接近座标(x,y) 最上层的一个项目的tuple, halo 代表涵盖的距离范围, 项目限定在start 项目之下 |
find_enclosed(x1, y1, x2, y2) 返回在区域中 (x1, y1), (x2, y2) 完全被涵盖的所有项目 |
find_overlapping(x1, y1, x2, y2) |
find_withtag(tagOrId) 返回 tagOrId 中所有的项目 |
focus(tagOrId=None) 聚焦于 tagOrId 第一个项目, 或者返回目前聚焦的项目, 如果没有聚焦的项目, 则返回空字符串 |
gettags(tagOrId) 返回 tagOrId 中最低项目的所有标签 |
icursor(tagOrId, index) 设置可聚焦文本项目的插入光标位置索引 |
index(tagOrId, specifier) 返回 tagOrId 最下层文本项目的specifier 索引值, specifier :1. tk.INSERT - 目前的位置2. tk.END - 最后的位置.3. tk.SEL_FIRST - 选择的起点, 没有则出错tk .TclError.4. tk.SEL_LAST - 选择的终点, 没有则出错tk.TclError.5. "@x,y" - 在座标(x, y) 的索引 |
insert(tagOrId, specifier, text) 在 tagOrId 最下层文本项目中, 于 specifier 处插入字符串 text |
itemcget(tagOrId, option) 返回 tagOrId 最下层项目的选项 option 值 |
itemconfigure(tagOrId, option, …) 设置或返回 tagOrId 最下层项目的选项 option 值 |
move(tagOrId, xAmount, yAmount) 移动 tagOrId 的项目, 位置偏移量为 xAmount , yAmount |
postscript(option, …) 以Encapsulated PostScript 格式返回画布的内容, 选项options: colormode 色彩模式- color 彩色/gray 灰度/ mono 黑白,file - 储存文件名, 没有设置则返回字符串height - 画布高度rotate - 横式(False) or 直式(True) 方向x - 打印的最左方座标y -打印的最上方座标width - 画布寛度 |
scale(tagOrId, xOffset, yOffset, xScale, yScale) 以(xoffset, yoffset) 为参考点来缩放 tagOrId 中的项目, 缩放比率为xScale 及yScale , 文本项目不会缩放, 但位置可能会改变 |
scan_dragto(x, y, gain=10.0) 鼠标键按下事件处理程序中已标记位置时, 鼠标滚动事件处理程序拖弋卷动画布, (x, y) 为位置, gain 为画布卷动的速率,值越大越快 |
scan_mark(x, y) 鼠标键按下事件处理程序中标记位置 (x, y) |
select_adjust(oid, specifier) 调整项目 oid 的文本选择区以包含索引 specifier , 定位处也会重新设置 |
select_clear() 去除选择 |
select_from(oid, specifier) 设置项目 oid 选择区的起始索引处 specifier |
select_item() 返回目前选择区的项目识别码, 如果没有选择区, 则返回 None |
select_to(oid, specifier) 设置项目 oid 选择区的停止索引处 specifier |
tag_bind(tagOrId, sequence=None, function=None, add=None) 绑定 tagOrId 项目sequence 事件的处理程序function , add 为'+' 代表新增处理程序, 否则将取代所有旧的程序. 绑定对新增或移除的项目没有作用 |
tag_lower(tagOrId, belowThis) 在显示列表中移动 tagOrId 中的项目到 aboveThis 下方, 座标位置不变 |
tag_raise(tagOrId, aboveThis) 在显示列表中移动 tagOrId 中的项目到 aboveThis 上方, 座标位置不变 |
tag_unbind(tagOrId, sequence, funcId=None) 移除 tagOdID 项目对 sequence 事件的绑定函数 funcId |
type(tagOrId) 返回 tagOrId 中第一个项目的类型, arc , bitmap , image , line , oval , polygon , rectangle , text , 或window |
xview(tk.MOVETO, fraction) 供水平滚动条 scrollbar 选项command 使用, fraction 0.0 为最左处, 1.0 为右处 |
xview(tk.SCROLL, n, what) 水平滚动画布 n 个单位, 单位what 为tk.UNITS 或tk.PAGES , < code>tk.UNITS 的大小由画布的xscrollincrement 选项而定, tk.PAGES 的大小是画布度的9/10 |
xview_moveto(fraction) 同 xview(tk.MOVETO, fraction) |
xview_scroll(n, what) 同 .xview(tk.SCROLL, n, what) |
yview(tk.MOVETO, fraction) 垂直滚动画布, 类似 xview(tk.MOVETO,…) |
yview(tk.SCROLL, n, what) 垂直滚动画布, 类似 xview(tk.SCROLL,…) |
yview_moveto(fraction) 垂直滚动画布, 类似 xview() |
yview_scroll(n, what) 垂直滚动画布, 类似 xview(), xview_moveto(), and xview_scroll() |
范例视窗及代码
import math
import tkinter as tk
root = tk.Tk(className="canvas demo")
canvas = tk.Canvas(root, bg='green', width=640, height=480)
canvas.grid()
r = 220
x_offset, y_offset = 20, 240
points = [(x+x_offset, r*math.sin(x/90*math.pi)+y_offset) for x in range(600)]
ids = [canvas.create_oval(x-1, y-1, x+1, y+1, fill='white', outline='white')
for x, y in points]
line = {'arrow':tk.LAST, 'fill':'yellow', 'width':4}
line_x = canvas.create_line(10, 240, 630, 240, **line)
line_y = canvas.create_line(20, 470, 20, 10, **line)
x_label = canvas.create_text(620, 250, anchor=tk.E, fill='yellow',
text='Degree')
y_label = canvas.create_text( 25, 90, anchor=tk.NW, fill='yellow',
text='Sin Curve', angle=90)
# angle option added for new version tkinter.
root.mainloop()
本作品采用《CC 协议》,转载必须注明作者和本文链接