Python画图小案例之小雪人超详细源码注释
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Python画图⼩案例之⼩雪⼈超详细源码注释⼀步步教你怎么⽤Python画雪⼈,进⼀步熟悉Python的基础画图操作,废话不多说,上代码。
希望您给个关注给个赞,也算对我们的⽀持了。
class Shape: # 基类(雪⼈各部件(形状)共有的属性)
def __init__(self, cvns, points, fill): # 构造⽅法画布位置坐标颜⾊
self.cvns = cvns # 画布
self.points = points # 坐标(x1, y1, x2, y2)
self.fill = fill
self.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,fill
self.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_point
self.w = w
self.fill = fill
self.h = h
self.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 / 2
x1 = self.start_point[0] + self.w / 2 - r
y1 = self.start_point[1] + 20 - r
x2 = x1 + 2 * r
y2 = y1 + 2 * r
return x1, y1, x2, y2
def hb_cacu(self): # 计算底部坐标(三⾓形的三个点的坐标)
x1 = self.start_point[0] + self.w / 2
y1 = self.start_point[1] + self.h / 3
x2 = self.start_point[0] + self.w / 3
y2 = self.start_point[1] + self.h + 13
x3 = self.start_point[0] + self.w / 3 * 2
y3 = y2
return 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 = cvns
self.start_point = start_point
self.fill = fill
self.w = w
self.h = h
eye0_points = self.eye0_cacu() # 眼睛1坐标
dx = self.h / 3 + self.h / 9
eye1_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) / 2
y1 = self.start_point[1]
x2 = x1 + self.h
y2 = y1 + self.h
return 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 / 6
y1 = left_point[1] + self.h / 3
x2 = x1 + self.h / 3
y2 = left_point[1] + self.h / 2
return 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 / 3
y1 = left_point[1] + 2 * self.h / 3 + 25 # +25后⼝的位置靠下,并且图形更⼤了 x2 = x1 + self.h / 3
y2 = left_point[1] + self.h / 2
return 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 = cvns
self.start_point = start_point
self.w = w
self.h = h
self.fill = fill
self._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 + w
y1 = self.start_point[1] + self.h / 2
x2 = x1 + 30
y2 = y1 - 26 * self._button_size
return 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_point
x2 = x1 + self.w
y2 = y1 + self.h
return x1, y1, x2, y2
def button0_cacu(self): # 计算第0个钮扣的坐标
x1 = self.start_point[0] + self.w / 2 - self._button_size
y1 = self.start_point[1] + self.h / 5 - self._button_size
x2 = x1 + 2 * self._button_size # 2决定钮扣的园形形状
y2 = y1 + 2 * self._button_size
return x1, y1, x2, y2
def move_dy(self, points, size): # 钮扣移动的⽅法
y1 = points[1] + size
y2 = points[3] + size
return points[0], y1, points[2], y2
def all_button_points(self): # 绘制每个钮扣的坐标
b0_points = self.button0_cacu()
size = self.h / 6 # ⾝⾼/钮扣数+1
points = [] # 列表
for i in range(5): # 钮扣的个数
points.append(self.move_dy(b0_points, i * size)) # 各钮扣的移动数据存⼊列表points
return 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 = cvns
self.points = points
self.w = w
self.h = h
self.fill = fill
self.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 tkinter
root = () # 建⽴根窗⼝
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 雪⼈内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!。