痛苦啊!tkinter资料不是很多,学习了3天了,至今没找到官方文档,画arc时遇到了困难,不知道起始角度怎么写,找了好多地方都没有介绍,最终无意间找到了小甲鱼整理的文档,终于解决。
此外,tkinter的画圆、弧、椭圆的方式,实在是太业余了。用起来很不顺手。难受!
思路:
工具展示:
1.鼠标点哪里就显示哪里的坐标,严格来说应该做一下canvasx(),canvasy()坐标系转换的,懒得做了。
2.点change按钮可以调整图层前后,方便查看所画图形和原图契合度
所画图形在前
所画图形在后
绘制过程中的代码:
from tkinter import *
#初始化
root=Tk()
global showflag
v=[IntVar(),IntVar()]
showflag=False #函数hide()所用
w=Canvas(root,width=400,height=400,bg="pink")
def drawcircle(r,x,y,color):
#自定义画圆函数,参数为:半径、圆心X,圆心Y,填充颜色
rhead1=r
xhead1=x
yhead1=y
w.create_oval(xhead1-rhead1,yhead1-rhead1,xhead1+rhead1,
yhead1+rhead1,fill=color)
#临时设置的背景原图,方便查看我画的和原图的契合度
img=PhotoImage(file=r'C:\Users\Administrator.USER-20180904HO\Desktop\python\ameng.gif')
cankao1=w.create_image((204,203),image=img)
#辅助线
w.create_line(0,200,400,200,fill="gray",dash=(4))
w.create_line(200,0,200,400,fill="gray",dash=(4))
# 画头部(眼睛部分都是对称的,因为只画一次,直接写数字了。)
drawcircle(90,200,139,"dodgerblue") #头外圈
drawcircle(75,200,154,"white") #头内圈
w.create_oval(163,64,200,109,fill="white") ##眼眶外圈
w.create_oval(400-163,64,400-200,109,fill="white")
w.create_oval(185,79,196,93,fill="black") #黑眼球
w.create_oval(400-185,79,400-196,93,fill="black")
w.create_oval(189,82,193,90,fill="white") #黑眼球反白
w.create_oval(400-189,82,400-193,90,fill="white")
drawcircle(7,200,105,"red") #鼻子
w.create_line(200,112,200,178,fill="black")#裂唇
w.create_line(148,113,185,125,fill="black")#胡子
w.create_line(140,136,185,136,fill="black")
w.create_line(148,159,185,147,fill="black")
w.create_line(400-148,113,400-185,125,fill="black")
w.create_line(400-140,136,400-185,136,fill="black")
w.create_line(400-148,159,400-185,147,fill="black")
w.create_arc(135,50,265,178,start=-48,extent=-84,style=ARC)#嘴巴
#画身体
w.create_rectangle(133,205,267,308,fill="dodgerblue") #主体
w.create_polygon((133,208),(133,246),(116,254),(106,236),outline="black",fill="dodgerblue") #手臂
w.create_polygon((400-133,208),(400-133,246),(400-116,254),(400-106,236),outline="black",fill="dodgerblue")
drawcircle(15,102,250.5,"white")#手
drawcircle(15,400-102,250.5,"white")
w.create_oval(117,300,192,329,fill="white")#脚
w.create_oval(400-117,300,400-192,329,fill="white")
w.create_arc(188,301,212,325,start=30,extent=120,outline="white",style=PIESLICE,fill="white")
#裤裆,先用白边白色扇形将蓝色擦除
w.create_arc(188,301,212,325,start=30,extent=120,style=ARC)
#裤裆,再用黑边白色弧形将描裤裆边缘 /:笑哭
#画服饰
drawcircle(52,200,240,"white")#白肚皮
w.create_arc(148,188,252,292,start=45,extent=90,style=ARC,outline="white",width=3)
#用宽一些的白线擦掉白肚皮上面不需要的部分
w.create_line(133,202,267,202,capstyle=ROUND,fill="black",width=12)
#小围巾,想做到原图的黑边红里效果,就画了一根粗黑线和一根细红线,边缘正好差1个像素。
w.create_line(133,202,267,202,capstyle=ROUND,fill="red",width=10)
#小围巾,细红线
w.create_arc(163,201,237,275,extent=-180,style=PIESLICE,fill="white")
#肚兜
drawcircle(11,200,212,"yellow")
#小铃铛
drawcircle(2.5,200,217,"red")
#小铃铛装饰
w.create_line(200,219,200,223,fill="black")
w.create_line(190,212,212,212,fill="black")
w.create_line(191,209,211,209,fill="black")
def location(event):
#定义一个关联事件,当鼠标点击时,显示点击坐标,方便找点
v[0]=event.x
v[1]=event.y
print("坐标:",v[0],v[1])
w.bind('' ,location)
def hide():
#设计一个button来回切换背景和所画图形的位置,以此查看契合度。
global showflag
if showflag==True:
w.lower(cankao1)
showflag=False
else:
w.lift(cankao1)
showflag=True
Button(root,text="change",command=hide).pack(side=BOTTOM)
w.pack()
mainloop()
绘制完成后,注释掉工具
最终代码
from tkinter import *
#初始化
root=Tk()
'''global showflag
v=[IntVar(),IntVar()]
showflag=False #函数hide()所用'''
w=Canvas(root,width=400,height=400,bg="pink")
def drawcircle(r,x,y,color):
#自定义画圆函数,参数为:半径、圆心X,圆心Y,填充颜色
rhead1=r
xhead1=x
yhead1=y
w.create_oval(xhead1-rhead1,yhead1-rhead1,xhead1+rhead1,
yhead1+rhead1,fill=color)
#临时设置的背景原图,方便查看我画的和原图的契合度
#img=PhotoImage(file=r'C:\Users\Administrator.USER-20180904HO\Desktop\python\ameng.gif')
#cankao1=w.create_image((204,203),image=img)
#辅助线
w.create_line(0,200,400,200,fill="gray",dash=(4))
w.create_line(200,0,200,400,fill="gray",dash=(4))
# 画头部(眼睛部分都是对称的,因为只画一次,直接写数字了。)
drawcircle(90,200,139,"dodgerblue") #头外圈
drawcircle(75,200,154,"white") #头内圈
w.create_oval(163,64,200,109,fill="white") ##眼眶外圈
w.create_oval(400-163,64,400-200,109,fill="white")
w.create_oval(185,79,196,93,fill="black") #黑眼球
w.create_oval(400-185,79,400-196,93,fill="black")
w.create_oval(189,82,193,90,fill="white") #黑眼球反白
w.create_oval(400-189,82,400-193,90,fill="white")
drawcircle(7,200,105,"red") #鼻子
w.create_line(200,112,200,178,fill="black")#裂唇
w.create_line(148,113,185,125,fill="black")#胡子
w.create_line(140,136,185,136,fill="black")
w.create_line(148,159,185,147,fill="black")
w.create_line(400-148,113,400-185,125,fill="black")
w.create_line(400-140,136,400-185,136,fill="black")
w.create_line(400-148,159,400-185,147,fill="black")
w.create_arc(135,50,265,178,start=-48,extent=-84,style=ARC)#嘴巴
#画身体
w.create_rectangle(133,205,267,308,fill="dodgerblue") #主体
w.create_polygon((133,208),(133,246),(116,254),(106,236),outline="black",fill="dodgerblue") #手臂
w.create_polygon((400-133,208),(400-133,246),(400-116,254),(400-106,236),outline="black",fill="dodgerblue")
drawcircle(15,102,250.5,"white")#手
drawcircle(15,400-102,250.5,"white")
w.create_oval(117,300,192,329,fill="white")#脚
w.create_oval(400-117,300,400-192,329,fill="white")
w.create_arc(188,301,212,325,start=30,extent=120,outline="pink",style=PIESLICE,fill="pink")
#裤裆,先用白边白色扇形将蓝色擦除,最终完成时修改为粉色
w.create_arc(188,301,212,325,start=30,extent=120,style=ARC)
#裤裆,再用黑边白色弧形将描裤裆边缘 /:笑哭
#画服饰
drawcircle(52,200,240,"white")#白肚皮
w.create_arc(148,188,252,292,start=45,extent=90,style=ARC,outline="white",width=3)
#用宽一些的白线擦掉白肚皮上面不需要的部分
w.create_line(133,202,267,202,capstyle=ROUND,fill="black",width=12)
#小围巾,想做到原图的黑边红里效果,就画了一根粗黑线和一根细红线,边缘正好差1个像素。
w.create_line(133,202,267,202,capstyle=ROUND,fill="red",width=10)
#小围巾,细红线
w.create_arc(163,201,237,275,extent=-180,style=PIESLICE,fill="white")
#肚兜
drawcircle(11,200,212,"yellow")
#小铃铛
drawcircle(2.5,200,217,"red")
#小铃铛装饰
w.create_line(200,219,200,223,fill="black")
w.create_line(190,212,212,212,fill="black")
w.create_line(191,209,211,209,fill="black")
'''def location(event):
#定义一个关联事件,当鼠标点击时,显示点击坐标,方便找点
v[0]=event.x
v[1]=event.y
print("坐标:",v[0],v[1])
w.bind('',location)
def hide():
#设计一个button来回切换背景和所画图形的位置,以此查看契合度。
global showflag
if showflag==True:
w.lower(cankao1)
showflag=False
else:
w.lift(cankao1)
showflag=True
Button(root,text="change",command=hide).pack(side=BOTTOM)
'''
w.pack()
mainloop()
最终效果: