Python画图小案例之小雪人超详细源码注释
一步步教你怎么用Python画雪人,进一步熟悉Python的基础画图操作,废话不多说,上代码。
希望您给个关注给个赞,也算对我们的支持了。
class Shape:# 基类(雪人各部件(形状)共有的属性)def __init__(self, cvns, points, fill):# 构造方法画布位置坐标颜色self.cvns = cvns# 画布self.points = points# 坐标(x1, y1, x2, y2)self.fill = fillself.pid = None# 当前图形的id def delete(self):# 删除图形if self.pid:self.cvns.delete(self.pid) class ShapeAngles(Shape):# 继承基类(增加了角度))def __init__(self, cvns, points, fill, angles=(10, 170)):# angles:角度值,带默认参数super(ShapeAngles, self).__init__(cvns, points, fill)# 调用基类构造: cvns,points,fillself.angles = {'start':angles[0], 'extent':angles[1]}# 构造自己的属性:angles class HatTop(Shape):# 帽子顶部def draw(self):#self.pid = self.cvns.create_oval(self.points, fill='white')# 椭圆形self.pid = self.cvns.create_oval(self.points, fill=self.fill)# 椭圆形 class HatBottom(Shape):# 帽子底部def draw(self):self.pid = self.cvns.create_polygon(self.points)# 绘多边形的方法 class Hat:# 帽子整体(组合顶部和底部)def __init__(self, cvns, start_point, fill, w, h):# w,h是帽子的宽、高self.cvns = cvns# 初始化self.start_point = start_pointself.w = wself.fill = fillself.h = hself.ht = HatTop(self.cvns, self.ht_cacu(), fill=self.fill)# 实例化顶部self.hb = HatBottom(self.cvns, self.hb_cacu(), self.fill)# 实例化底部def draw(self):# 绘制self.ht.draw()# 调用顶部方法绘制self.hb.draw()# 调用底部方法绘制 def delete(self):self.ht.delete()# self.hb.delete() def ht_cacu(self):# 计算顶部坐标r = self.h / 3 / 2x1 = self.start_point[0] + self.w / 2 - ry1 = self.start_point[1] + 20 - rx2 = x1 + 2 * ry2 = y1 + 2 * rreturn x1, y1, x2, y2 def hb_cacu(self):# 计算底部坐标(三角形的三个点的坐标)x1 = self.start_point[0] + self.w / 2y1 = self.start_point[1] + self.h / 3x2 = self.start_point[0] + self.w / 3y2 = self.start_point[1] + self.h + 13x3 = self.start_point[0] + self.w / 3 * 2y3 = y2return x1, y1, x2, y2, x3, y3 class Sense(ShapeAngles):# 五官(眼、口扇形图形)def draw(self):self.pid = self.cvns.create_arc(*self.points, **self.angles, fill='red')# 绘制弧线 class Face(HatTop):# 脸pass class Head:# 头部def __init__(self, cvns, start_point, fill, w, h):# 此处的w,h是头的self.cvns = cvnsself.start_point = start_pointself.fill = fillself.w = wself.h = heye0_points = self.eye0_cacu()# 眼睛1坐标dx = self.h / 3 + self.h / 9eye1_points = (eye0_points[0] + dx, eye0_points[1],# 眼睛2坐标eye0_points[2] + dx, eye0_points[3])self.face = Face(self.cvns, self.face_cacu(), self.fill)# 脸:带参数的实例self.eye0 = Sense(self.cvns, eye0_points, fill='blue')# 眼1:带参数的实例self.eye1 = Sense(self.cvns, eye1_points, self.fill)# 眼2:带参数的实例self.mouth = Sense(self.cvns, self.mouth_cacu(), (-10, -170))# 口:带参数的实例 def draw(self):# 绘制脸部各部位self.face.draw()self.eye0.draw()self.eye1.draw()self.mouth.draw() def face_cacu(self):# 脸坐标计算x1 = self.start_point[0] + (self.w - self.h) / 2y1 = self.start_point[1]x2 = x1 + self.hy2 = y1 + self.hreturn x1, y1, x2, y2 def eye0_cacu(self):# 眼0坐标计算left_point = (self.start_point[0] + (self.w - self.h) / 2 - 5, self.start_point[1])x1 = left_point[0] + self.h / 6y1 = left_point[1] + self.h / 3x2 = x1 + self.h / 3y2 = left_point[1] + self.h / 2return x1, y1, x2, y2 def mouth_cacu(self):# 口坐标计算left_point = (self.start_point[0] + (self.w - self.h) / 2, self.start_point[1])x1 = left_point[0] + self.h / 3y1 = left_point[1] + 2 * self.h / 3 + 25# +25后口的位置靠下,并且图形更大了x2 = x1 + self.h / 3y2 = left_point[1] + self.h / 2return x1, y1, x2, y2 class hand(HatTop):# 手pass class BodyOutline(HatTop):# 身体轮廓,因没有特别的形状,继承了基类,类体为空pass class Button(HatTop):# 钮扣pass class Body:# 身体 def __init__(self, cvns, start_point, fill, w, h):self.cvns = cvnsself.start_point = start_pointself.w = wself.h = hself.fill = fillself._button_size = 10# 钮扣的大小self.buttons = []self.bo = BodyOutline(self.cvns, self.body_cacu(), self.fill)# 身体轮廓实例#self.hd = hand(self.cvns, (15, 500, 45, 240), self.fill)# 左手轮廓实例,坐标为矩形的两个对角顶点的坐标为准画的圆/椭圆self.hd = hand(self.cvns, self.bd_cacu(0), self.fill)# 左手轮廓实例,坐标为矩形的两个对角顶点的坐标为准画的圆/椭圆self.hd2 = hand(self.cvns, self.bd_cacu(self.w), self.fill)# 右手for pnts in self.all_button_points():self.buttons.append(Button(self.cvns, pnts, self.fill)) def bd_cacu(self, w):# 计算手的坐标x1 = 15 + wy1 = self.start_point[1] + self.h / 2x2 = x1 + 30y2 = y1 - 26 * self._button_sizereturn x1, y1, x2, y2 def draw(self):self.bo.draw()# 身体绘制self.hd.draw()# 手1绘制self.hd2.draw()# 手2绘制for bttn in self.buttons:# 各钮扣绘制bttn.draw() def body_cacu(self):# 计算身体轮廓坐标x1, y1 = self.start_pointx2 = x1 + self.wy2 = y1 + self.hreturn x1, y1, x2, y2 def button0_cacu(self):# 计算第0个钮扣的坐标x1 = self.start_point[0] + self.w / 2 - self._button_sizey1 = self.start_point[1] + self.h / 5 - self._button_sizex2 = x1 + 2 * self._button_size# 2决定钮扣的园形形状y2 = y1 + 2 * self._button_sizereturn x1, y1, x2, y2 def move_dy(self, points, size):# 钮扣移动的方法y1 = points[1] + sizey2 = points[3] + sizereturn points[0], y1, points[2], y2 def all_button_points(self):# 绘制每个钮扣的坐标b0_points = self.button0_cacu()size = self.h / 6# 身高/钮扣数+1points = []# 列表for i in range(5):# 钮扣的个数points.append(self.move_dy(b0_points, i * size))# 各钮扣的移动数据存入列表pointsreturn points# 返回列表值 # def set_button_size(self, size):#self._button_size = size class Snow:# 组装成雪人 def __init__(self, cvns, points, fill, w=150, h=450):# points为雪人的坐标其与帽子坐标一致(见雪人图)self.cvns = cvnsself.points = pointsself.w = wself.h = hself.fill = fillself.head = Head(self.cvns, (self.points[0], self.points[1] + self.h / 6), self.fill, self.w, self.h / 3)# 实例化头部self.body = Body(self.cvns, (self.points[0], self.points[1] + self.h / 2), self.fill, self.w, self.h / 2)# 实例化身体self.fill = 'red'# 帽子顶部颜色self.hat = Hat(self.cvns, self.points, self.fill, self.w, self.h / 6)# 绘帽子# 实例化帽子 def draw(self):self.hat.draw()# 绘制帽子self.head.draw()# 绘制头self.body.draw()# 绘制身体 if __name__ == '__main__':import tkinterroot = tkinter.Tk()# 建立根窗口cvns = tkinter.Canvas(root, width=400, height=700, bg='white')# 调用画布cvns.pack()# 将画布添加到窗口snow = Snow(cvns, (30, 15), 'white', 320, 660)# 雪人的实例化(传入画布对象、起始坐标、宽、高)snow = snow.draw()# 绘制root.mainloop()
【Python画图小案例之小雪人超详细源码注释】到此这篇关于Python画图小案例之小雪人超详细源码注释的文章就介绍到这了,更多相关Python 雪人 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
推荐阅读
- 一个小故事,我的思考。
- 家乡的那条小河
- 一个人的碎碎念
- 野营记-第五章|野营记-第五章 讨伐梦魇兽
- 昨夜小楼听风
- 2021-02-17|2021-02-17 小儿按摩膻中穴-舒缓咳嗽
- 基于微信小程序带后端ssm接口小区物业管理平台设计
- 2019.4.18感恩日记
- 那件我们忽略的小事叫感恩
- 你有婚内虐待行为吗()