之前学习HTML5中Canvas绘图方法时,为测试函数功能,使用JavaScript在Canvas中绘制了多种工程图符号,下面两张图是工程图符号的原图。本文参照JavaScript绘图程序,使用python在Tkinter模块的Canvas控件中也绘制相同工程符号,以加深对tkinter绘图函数的理解。
文本选取粗糙度、直线度、平面度、平行度、垂直度、倾斜度、对称度、圆度、圆柱度、轮廓度、面轮廓度、同轴度、位置度等十余种工程符号进行绘制,主要涉及的绘图函数包括:
下面是程序的源码及运行结果,最麻烦的是计算每种工程符号的坐标信息,由于使用JavaScript绘制HTML5的Canvas图形的函数及参数意义与在Python不同,即便是参考之前的JavaScript绘图代码,也需要重新计算每种绘图参数中的坐标信息。
# coding=gbk
import tkinter as tk
window=tk.Tk()
window.title('Canvas测试')
window.geometry('500x800')
tk.Label(window,text='工程图示例').grid(row=0,column=0)
cv=tk.Canvas(window,bg="lightblue",width =400, height = 700)
cv.grid(row=1,column=1)
#粗糙度A
cv.create_text(20,50,text ='绘制粗糙度:',font =('宋体',8,'bold'),anchor = tk.W)
cv.create_line(150,50,160,70,170,30)
cv.create_line(150,50,165,50)
#粗糙度B
cv.create_line(200,50,210,70,220,30)
#粗糙度C
cv.create_line(250,50,260,70,270,30)
cv.create_oval(255,50,264,58,fill ='',outline = 'black')
#直线度
cv.create_text(20,100,text ='绘制直线度:',font =('宋体',8,'bold'),anchor = tk.W)
cv.create_line(150,100,170,100)
#平面度
cv.create_text(20,150,text ='绘制平面度:',font =('宋体',8,'bold'),anchor = tk.W)
cv.create_line(150,150,170,150,165,160,145,160,150,150)
#平行度
cv.create_text(20,200,text ='绘制平行度:',font =('宋体',8,'bold'),anchor = tk.W)
cv.create_line(150,200,145,210)
cv.create_line(160,200,155,210)
#垂直度
cv.create_text(20,250,text ='绘制垂直度:',font =('宋体',8,'bold'),anchor = tk.W)
cv.create_line(150,245,150,260)
cv.create_line(140,260,160,260)
#倾斜度
cv.create_text(20,300,text ='绘制倾斜度:',font =('宋体',8,'bold'),anchor = tk.W)
cv.create_line(150,300,165,300)
cv.create_line(150,300,165,290)
#对称度
cv.create_text(20,350,text ='绘制对称度:',font =('宋体',8,'bold'),anchor = tk.W)
cv.create_line(150,350,170,350)
cv.create_line(155,345,165,345)
cv.create_line(155,355,165,355)
#圆度
cv.create_text(20,400,text ='绘制圆度:',font =('宋体',8,'bold'),anchor = tk.W)
cv.create_oval(150,385,180,415,fill ='',outline = 'black')
#圆柱度
cv.create_text(20,450,text ='绘制圆柱度:',font =('宋体',8,'bold'),anchor = tk.W)
cv.create_oval(150,437.5,175,462.5,fill ='',outline = 'black')
cv.create_line(125,465,170,427)
cv.create_line(145,480,198,436)
#轮廓度
cv.create_text(20,500,text ='绘制轮廓度:',font =('宋体',8,'bold'),anchor = tk.W)
cv.create_arc(150,500,180,530,start = 0, extent = 180, fill = '',outline ='black',style="arc")
#面轮廓度
cv.create_text(20,550,text ='绘制轮廓度:',font =('宋体',8,'bold'),anchor = tk.W)
cv.create_arc(150,550,180,580,start = 0, extent = 180, fill = '',outline ='black',style="chord")
#同轴度
cv.create_text(20,600,text ='绘制同轴度:',font =('宋体',8,'bold'),anchor = tk.W)
cv.create_oval(150,585,180,615,fill ='',outline = 'black')
cv.create_oval(157.5,592.5,172.5,607.5,fill ='',outline = 'black')
#位置度
cv.create_text(20,650,text ='绘制位置度:',font =('宋体',8,'bold'),anchor = tk.W)
cv.create_oval(150,635,180,665,fill ='',outline = 'black')
cv.create_line(135,650,195,650)
cv.create_line(165,620,165,680)
window.mainloop()
参考文献
[1]https://jingyan.baidu.com/article/49711c61a9dbddfa441b7c0b.html
[2]https://blog.csdn.net/weixin_44940488/article/details/124062536