python tkinter canvas

Canvas 的坐标系统是绘图的基础,其中点 (0,0) 位于 Canvas 组件的左上角,X 轴水平向右延伸,Y 轴垂直向下延伸。

  • create_arc:绘制弧。
  • create_bitmap:绘制位图。
  • create_image:绘制图片。
  • create_line():绘制直线。
  • create_polygon:绘制多边形。
  • create_text:绘制文字。
  • create_window:绘制组件。
  •  create_rectangle() 方法绘制矩形
  • create_oval() 方法绘制椭圆(包括圆,圆是椭圆的特例)

绘制上面这些图形时需要简单的几何基础:

  • 在使用 create_line() 绘制直线时,需要指定两个点的坐标,分别作为直线的起点和终点。
  • 在使用 create_rectangle() 绘制矩形时,需要指定两个点的坐标,分别作为矩形左上角点和右下角点的坐标。
  • 在使用 create_oval() 绘制椭圆时,需要指定两个点的坐标,分别作为左上角点和右下角点的坐标来确定一个矩形,而该方法则负责绘制该矩形的内切椭圆,如图 2 所示。


图 2 内切椭圆


从图 2 可以看出,只要矩形确定下来,该矩形的内切椭圆就能确定下来,而 create_oval() 方法所需要的两个坐标正是用于指定该矩形的左上角点和右下角点的坐标。

在使用 create_arc 绘制弧时,和 create_oval 的用法相似,因为弧是椭圆的一部分,因此同样也是指定左上角和右下角两个点的坐标,默认总是绘制从 3 点(0)开始,逆时针旋转 90° 的那一段弧。程序可通过 start 改变起始角度,也可通过 extent 改变转过的角度。

在使用 create_polygon 绘制多边形时,需要指定多个点的坐标来作为多边形的多个定点;在使用 create_bitmap、create_image、create_text、create_window 等方法时,只要指定一个坐标点,用于指定目标元素的绘制位置即可。

在绘制这些图形时可指定如下选项:

    • fill:指定填充颜色。如果不指定该选项,默认不填充。
    • outline:指定边框颜色。
    • width:指定边框宽度。如果不指定该选项,边框宽度默认为 1。
    • dash:指定边框使用虚线。该属性值既可为单独的整数,用于指定虚线中线段的长度;也可为形如(5,2,3)格式的元素,此时5 指定虚线中线段的长度,2 指定间隔长度,3 指定虚线长度……依此类推。
    • stipple:使用位图平铺进行填充。该选项可与 fill 选项结合使用,fill 选项用于指定位图的颜色。
    • style:指定绘制弧的样式。该选项仅对 create_arc 方法起作用。该选项支持 PIESLICE(扇形)、CHORD(弓形)、ARC(仅绘制弧)选项值。
    • start:指定绘制弧的起始角度。该选项仅对 create_arc 方法起作用。
    • extent:指定绘制弧的角度。该选项仅对 create_arc 方法起作用。
    • arrow:指定绘制直线时两端是否有箭头。该选项支持 NONE(两端无箭头)、FIRST(开始端有箭头)、LAST(结束端有箭头)、BOTH(两端都有箭头)选项值。
    • arrowshape:指定箭头形状。该选项是一个形如 "20 20 10" 的字符串,字符串中的三个整数依次指定填充长度、箭头长度、箭头宽度。
    • joinstyle:指定直接连接点的风格。仅对绘制直线和多向形有效。该选项支持 METTER、ROUND、BEVEL 选项值。
    • anchor:指定绘制文字、GUI 组件的位置。该选项仅对 create_text()、create_window() 方法有效。
    • justify:指定文字的对齐方式。该选项支持 CENTER、LEFT、RIGHT 常量值,该选项仅对 create_text 方法有效。
from tkinter import *

root = Tk()

root.title('missWjz')

cv = Canvas(root,background='white',width=830,height=830)
cv.pack(fill=BOTH,expand=YES)

#对字体进行初始化,字体样式,字体大小,字体是否加粗
columnFont = ('微软雅黑',18)
titleFont = ('微软雅黑',15,'bold')

#采用循环打印字体
for i,str in enumerate(['默认', '指定边宽', '指定填充', '边框颜色', '位图填充']):
    cv.create_text((130 + i*140,20),text=str,
                   font = columnFont,   #字体
                   fill = 'red',        #填充颜色
                   anchor = W,          #对齐方式
                   justify = LEFT
                   )


#绘制字体
cv.create_text((10,80),text="绘制矩形",
               font = titleFont,
               fill = 'blue',
               anchor = W,
               justify = LEFT
            )

#创建列表:图形边框大小 填充色 边框颜色 位图填充
options = [(None,None,None,None),
           (4,None,None,None),
           (4,'pink',None,None),
           (4,'pink','red',None),
           (4,'pink','red','error')]

#创建矩形图案
for i,opt in enumerate(options):
    cv.create_rectangle((130+i*140,50,240+i*140,120),
                        width = opt[0],     #边框宽度
                        fill = opt[1],      #图案填充色
                        outline = opt[2],   #图案边框颜色
                        stipple = opt[3]    #位图填充
                        )

#绘制字体
cv.create_text((10,190),text='绘制椭圆',
               font = titleFont,
               fill = 'blue',
               anchor = W,
               justify = LEFT
               )

#绘制椭圆
for i,opt in enumerate(options):
    cv.create_oval((130+i*140,150,240+i*140,220),
                   width = opt[0],
                   fill = opt[1],
                   outline = opt[2],
                   stipple = opt[3])

#绘制文字
cv.create_text((10,300),text='绘制多边形',
               font = titleFont,
               fill = 'blue',
               anchor = W,
               justify = LEFT)

#绘制 五角星
for i,opt in enumerate(options):
    cv.create_polygon((130+i*140,270,240+i*140,270,152+i*140,350,185+i*140,230,218+i*140,350,130+i*140,270),
                      width = opt[0],
                      fill = opt[1],
                      outline = opt[2],
                      stipple = opt[3])

#绘制文字
cv.create_text((10,410),text='绘制扇形',
               font = titleFont,
               fill = 'blue',
               anchor = W,
               justify = LEFT)

#绘制扇形 提供两个坐标
for i,opt in enumerate(options):
    cv.create_arc((130+i*140,380,240+i*140,460),
                  width = opt[0],
                  fill = opt[1],
                  outline = opt[2],
                  stipple = opt[3],
                  )

#绘制文字
cv.create_text((10,520),text='绘制弓形',
               font = titleFont,
               fill = 'blue',
               anchor = W,
               justify = LEFT)
#绘制弓形
for i,opt in enumerate(options):
    cv.create_arc((130+i*140,490,240+i*140,570),
                  width = opt[0],
                  fill = opt[1],
                  outline = opt[2],
                  stipple = opt[3],
                  start = 30,   #起始角度
                  extent = 180, #逆时针转过角度
                  style =  CHORD    #弓张开的方式
                  )

#绘制文字
cv.create_text((10,630),text='仅绘弧',
               font = titleFont,
               fill = 'blue',
               anchor = W,
               justify = LEFT)
#仅绘制弧
for i,opt in enumerate(options):
    cv.create_arc((130+i*140,600,240+i*140,680),
                  width = opt[0],
                  fill = opt[1],
                  outline = opt[2],
                  stipple = opt[3],
                  start = 30,   #起始角度
                  extent = 180, #逆时针转过角度
                  style =  ARC    #弓张开的方式
                  )
#绘制文字
cv.create_text((10,740),text='绘制直线',
               font = titleFont,
               fill = 'blue',
               anchor = W,
               justify = LEFT)
#绘制直线
options = [(None,None,None,None,None),
           (6,None,None,BOTH,(20,40,10)),   #BOTH:两头都有箭头;长度,箭头长度,宽度
           (6,'pink',None,FIRST,(40,40,10)),
           (6,'pink',None,LAST,(60,50,10)),
           (8,'pink','error',None,None)]

for i,opt in enumerate(options):
    cv.create_line((130+i*140,710,240+i*140,790),
                   width = opt[0],
                   fill = opt[1],
                   stipple = opt[2],
                   arrow = opt[3],
                   arrowshape=opt[4]#arroeshape形如 "20 20 10" 的字符串,字符串中的三个整数依次指定填充长度、箭头长度、箭头宽度
                   )

root.mainloop()

效果图:

非学无以广才,非志无以成学。 正是因为今天的不完美,才对未来充满希望。 ----长帆
原文地址:https://www.cnblogs.com/changfan/p/10841884.html