Python画图基础操作之全注释画多啦A梦叮当猫

举报
zhulin1028 发表于 2021/10/29 22:31:55 2021/10/29
【摘要】 一步步教你怎么用Python画多啦A梦叮当猫,进一步熟悉Python的基础画图操作。 分析:叮当猫由头、脸、眼、眼珠、鼻子、嘴、胡子、项带、铃当、身子、围嘴、手臂、手、脚组成。 其中:头、脸、眼、眼珠、鼻子、嘴、胡子组成一个部件;其余元件组成一个部件。废话不多说,上代码。 希望您给个关注给个赞,也算对我们的支持了。 im...

一步步教你怎么用Python画多啦A梦叮当猫,进一步熟悉Python的基础画图操作。

分析:叮当猫由头、脸、眼、眼珠、鼻子、嘴、胡子、项带、铃当、身子、围嘴、手臂、手、脚组成。 其中:头、脸、眼、眼珠、鼻子、嘴、胡子组成一个部件;其余元件组成一个部件。废话不多说,上代码。

希望您给个关注给个赞,也算对我们的支持了。


  
  1. import math
  2. import sys
  3. from PyQt5.QtCore import *
  4. from PyQt5.QtGui import *
  5. from PyQt5.QtWidgets import *
  6. class Shape: # 图形基类(叮当猫各部件(形状)共有的属性)
  7. def __init__(self, qp, QRect, QColor=QColor("#07bbee")): # 构造方法参数:形状,位置坐标,颜色, color="#07bbee"
  8. self.qp = qp #qpainter()的实例
  9. self.rect = QRect # 坐标(x1, y1, x2, y2)
  10. self.color = QColor #颜色
  11. self.qp.setPen(QPen(Qt.black, 3)) #边线
  12. class Liner: #直线类
  13. def __init__(self, qp, QRect):
  14. self.qp = qp
  15. self.rect = QRect
  16. self.width = 1
  17. class Arc: #弧线类
  18. def __init__(self, qp, QRect, startAngle, spanAngle):
  19. self.qp = qp
  20. self.rect = QRect
  21. self.startAngle = startAngle
  22. self.spanAngle = spanAngle
  23. class eyeball(Shape): #眼珠
  24. def draw(self):
  25. self.qp.setBrush(self.color) #设置画刷
  26. self.qp.drawEllipse(self.rect) #画圆形x,y,w,h 120, 25, 160, 160
  27. class mouth(Arc):
  28. def draw(self):
  29. self.qp.drawArc(self.rect,self.startAngle,self.spanAngle) #后面两个参数分别为 起始角与跨度角
  30. class beard(Liner): #中央竖线
  31. def draw(self):
  32. self.qp.drawLine(self.rect)
  33. class Beards: #胡须组合
  34. def __init__(self, qp, start_point): # w,h 宽、高
  35. self.qp = qp #
  36. self.start_point = start_point #起始坐标
  37. self.bd0 = beard(self.qp, self.bd0_cacu())
  38. self.bd1 = beard(self.qp, self.bd1_cacu())
  39. self.bd2 = beard(self.qp, self.bd2_cacu())
  40. self.bd00 = beard(self.qp, self.bd00_cacu())
  41. self.bd11 = beard(self.qp, self.bd11_cacu())
  42. self.bd22 = beard(self.qp, self.bd22_cacu())
  43. def draw(self): # 绘制
  44. self.bd1.draw()
  45. self.bd0.draw()
  46. self.bd2.draw()
  47. self.bd11.draw()
  48. self.bd00.draw()
  49. self.bd22.draw()
  50. def bd0_cacu(self): # 计算胡须的坐标
  51. x1 = self.start_point[0] - 40
  52. y1 = self.start_point[1] - 5
  53. x2 = x1 - 58
  54. y2 = y1 - 20
  55. return QLineF(x1, y1, x2, y2)
  56. def bd1_cacu(self): # 计算中间胡须的坐标
  57. x1 = self.start_point[0] - 40
  58. y1 = self.start_point[1] + 5
  59. x2 = x1 -65
  60. y2 = y1
  61. return QLineF(x1, y1, x2, y2)
  62. def bd2_cacu(self): # 计算胡须的坐标
  63. x1 = self.start_point[0] - 40
  64. y1 = self.start_point[1] + 15
  65. x2 = x1 -58
  66. y2 = y1 + 20
  67. return QLineF(x1, y1, x2, y2)
  68. def bd00_cacu(self): # 计算胡须的坐标
  69. x1 = self.start_point[0] + 40
  70. y1 = self.start_point[1] - 5
  71. x2 = x1 + 58
  72. y2 = y1 - 20
  73. return QLineF(x1, y1, x2, y2)
  74. def bd11_cacu(self): # 计算胡须的坐标
  75. x1 = self.start_point[0] + 40
  76. y1 = self.start_point[1] + 5
  77. x2 = x1 + 65
  78. y2 = y1
  79. return QLineF(x1, y1, x2, y2)
  80. def bd22_cacu(self): # 计算胡须的坐标
  81. x1 = self.start_point[0] + 40
  82. y1 = self.start_point[1] + 15
  83. x2 = x1 + 58
  84. y2 = y1 + 20
  85. return QLineF(x1, y1, x2, y2)
  86. class head(Shape): # 头
  87. def draw(self):
  88. ##实例:x1,y1,x2,y2,即渐变的起始点和终止点
  89. linearGradient = QLinearGradient(420, 0, 100, 280)
  90. #线性渐变色的效果,以整个图形区间为100%,
  91. #分成多段设置颜色,各颜色按百分比分配。
  92. linearGradient.setColorAt(0.1, Qt.white) #10%处白色
  93. linearGradient.setColorAt(0.2, self.color) #60%处绿色 #07bbee
  94. linearGradient.setColorAt(0.8, self.color) #60%处绿色 #07bbee
  95. linearGradient.setColorAt(1.0, Qt.black) #100%处黑色
  96. self.qp.setBrush(linearGradient) #设置画刷
  97. # self.qp.setBrush(self.color)
  98. # qp.drawRoundedRect(120, 25, 160, 160, 40, 40, Qt.RelativeSize) #画圆角矩形x1,y1,x2,y2,圆角的角度
  99. self.qp.drawEllipse(self.rect) #画圆形x,y,w,h 120, 25, 160, 160
  100. class nose(Shape): #鼻子
  101. def draw(self):
  102. RadialGradient = QRadialGradient(265, 144, 6, 265, 144) #辐射渐变
  103. #参数分别为中心坐标,半径长度和焦点坐标,
  104. #如果需要对称那么中心坐标和焦点坐标要一致 #c93300
  105. #辐射渐变色的效果,以整个图形区间为100%,
  106. #分成多段设置颜色,各颜色按百分比分配。
  107. RadialGradient.setColorAt(0.0, Qt.white) #10%处白色
  108. RadialGradient.setColorAt(1.0, self.color) #60%处绿色 #07bbee
  109. # RadialGradient.setColorAt(1.0, Qt.black) #100%处黑色
  110. self.qp.setPen(QPen(Qt.black, 2)) #边线
  111. self.qp.setBrush(RadialGradient) #设置画刷
  112. self.qp.drawRoundedRect(self.rect, 80, 80, Qt.RelativeSize) #画圆角矩形x,y,w,h,圆角的角度
  113. class face(Shape):
  114. def draw(self):
  115. self.qp.setPen(Qt.NoPen) #无边线
  116. self.qp.setBrush(self.color) #设置画刷
  117. self.qp.drawEllipse(self.rect) #画圆形x,y,w,h 120, 25, 160, 160
  118. class eye(Shape): #眼框
  119. def draw(self):
  120. self.qp.setBrush(self.color) #设置画刷
  121. self.qp.drawRoundedRect(self.rect, 90, 90, Qt.RelativeSize) #画圆角矩形x1,y1,x2,y2,圆角的角度
  122. class Heads: # 头部整体(组合头、脸、眼、鼻、嘴、胡子)
  123. def __init__(self, qp, start_point,w, h): # w,h是帽子的宽、高
  124. self.qp = qp #
  125. self.start_point = start_point #起始坐标
  126. self.w = w
  127. self.h = h
  128. self.hd = head(self.qp, self.hd_cacu(), QColor('#07bbee')) # 例化头
  129. self.fc = face(self.qp, self.fc_cacu(), Qt.white) #脸
  130. self.nos = nose(self.qp, self.nos_cacu(), QColor("#c93300")) #鼻子
  131. self.eye0 = eye(self.qp, self.ey0_cacu(), Qt.white) # 实例化眼0
  132. self.eye1 = eye(self.qp, self.ey1_cacu(), Qt.white) #眼1
  133. self.bds = Beards(self.qp, (260, 185)) #胡须组合的起始位置
  134. self.bd = beard(self.qp, self.bd_cacu()) #中央竖线
  135. self.mt = mouth(self.qp, self.mt_cacu(), 230 * 16, 80 * 16) #弧线的起始角度, 弧线角度(角度*16)
  136. self.mt2 = mouth(self.qp, self.mt2_cacu(), 230 * 16, 80 * 16) #弧线的起始角度, 弧线角度(角度*16)
  137. self.eb0 = eyeball(self.qp, self.eb0_cacu(), Qt.black) #眼珠0
  138. self.eb1 = eyeball(self.qp, self.eb1_cacu(), Qt.black) #眼珠1
  139. def draw(self): # 绘制
  140. self.hd.draw() #调用头方法绘制
  141. self.fc.draw() # 调用脸方法绘制
  142. self.nos.draw() # 调用底部方法绘制
  143. self.eye0.draw()
  144. self.eye1.draw()
  145. self.bds.draw()
  146. self.bd.draw() #调用头方法绘制
  147. self.mt.draw()
  148. self.mt2.draw()
  149. self.eb0.draw() #眼珠
  150. self.eb1.draw()
  151. def eb0_cacu(self): # 计算眼珠的坐标
  152. x = self.start_point[0] + self.w / 2 - 25
  153. y = self.start_point[1] + 70
  154. w = 12
  155. h = 12
  156. return QRect(x, y, w, h)
  157. def eb1_cacu(self): # 计算眼珠的坐标
  158. x = self.start_point[0] + self.w / 2 + 15
  159. y = self.start_point[1] + 70
  160. w = 12
  161. h = 12
  162. return QRect(x, y, w, h)
  163. def bd_cacu(self): # 计算中央竖线的坐标
  164. x1 = self.start_point[0] + self.w / 2
  165. y1 = self.start_point[1] + 135
  166. x2 = x1
  167. y2 = y1 + 95
  168. return QLineF(x1, y1, x2, y2)
  169. def mt_cacu(self): # 计算嘴的坐标
  170. x = self.start_point[0] + 50
  171. y = self.start_point[1] + 45
  172. w = 220
  173. h = 186
  174. return QRect(x, y, w, h)
  175. def mt2_cacu(self): # 计算嘴的坐标
  176. x = self.start_point[0] + 60
  177. y = self.start_point[1] + 49
  178. w = 200
  179. h = 185
  180. return QRect(x, y, w, h)
  181. def nos_cacu(self): # 计算鼻子的坐标
  182. # r = self.h / 3 / 2
  183. x = self.start_point[0] + self.w /2 -15
  184. y = self.start_point[1] + 105
  185. w = 30
  186. h = 30
  187. return QRect(x, y, w, h)
  188. def hd_cacu(self): # 计算头的坐标
  189. x = self.start_point[0]
  190. y = self.start_point[1]
  191. w = self.w
  192. h = self.h
  193. return QRect(x, y, w, h)
  194. def fc_cacu(self): # 计算脸的坐标
  195. x = self.start_point[0] + 35
  196. y = self.start_point[1] + 65
  197. w = self.w - 65
  198. h = self.h - 100
  199. return QRect(x, y, w, h)
  200. def ey1_cacu(self): # 计算眼1的坐标(圆角矩形)
  201. x = self.start_point[0] + self.w / 2
  202. y = self.start_point[1] + 40
  203. w = 70
  204. h = 80
  205. return QRect(x, y, w, h)
  206. def ey0_cacu(self): # 计算眼0的坐标(圆角矩形)
  207. x = self.start_point[0] + self.w / 2 - 70
  208. y = self.start_point[1] + 40
  209. w = 70
  210. h = 80
  211. return QRect(x, y, w, h)
  212. class collar(Shape): #项圈
  213. def draw(self):
  214. linearGradient = QLinearGradient(140, 280, 140, 310)
  215. #参数分别为中心坐标,半径长度和焦点坐标,
  216. #如果需要对称那么中心坐标和焦点坐标要一致 #c93300
  217. #辐射渐变色的效果,以整个图形区间为100%,
  218. #分成多段设置颜色,各颜色按百分比分配。
  219. linearGradient.setColorAt(0.2, QColor("#c40")) #10%处
  220. linearGradient.setColorAt(1.0, QColor("#800400")) #
  221. # linearGradient.setColorAt(1.0, Qt.black) #100%处黑色
  222. self.qp.setPen(QPen(Qt.black, 2)) #边线
  223. self.qp.setBrush(linearGradient) #设置画刷
  224. self.qpp = QPainterPath() #路径
  225. self.qpp.moveTo(self.rect[0] + self.rect[2], self.rect[1] + 20)
  226. self.qpp.arcTo(self.rect[0] + self.rect[2] - 10, self.rect[1] , 20.0, 20.0, 270.0, 180.0) #(70, 30, R*2, R*2, 0, 180);(70,30)是起始位置,R是半径,270是起始角度,180是要画的角度
  227. self.qpp.lineTo(self.rect[0], self.rect[1])
  228. self.qpp.arcTo(self.rect[0]-10, self.rect[1], 20.0, 20.0, 90.0, 180.0)
  229. self.qpp.closeSubpath()
  230. self.qp.drawPath(self.qpp)
  231. class bell(Shape): #铃铛
  232. def draw(self):
  233. #参数分别为中心坐标,半径长度和焦点坐标,
  234. #如果需要对称那么中心坐标和焦点坐标要一致 #c93300
  235. RadialGradient = QRadialGradient(270, 308, 40, 260, 300) #辐射渐变
  236. #辐射渐变色的效果,以整个图形区间为100%,
  237. #分成多段设置颜色,各颜色按百分比分配。
  238. RadialGradient.setColorAt(0.0, QColor("#f9f12a")) #10%处白色
  239. RadialGradient.setColorAt(0.75, QColor("#e9e11a")) #60%处绿色 #07bbee
  240. RadialGradient.setColorAt(1.0, QColor("#a9a100")) #100%处黑色
  241. self.qp.setPen(QPen(Qt.black, 2)) #边线
  242. self.qp.setBrush(RadialGradient) #设置画刷
  243. self.qp.drawRoundedRect(self.rect, 90, 90, Qt.RelativeSize) #画圆角矩形x1,y1,x2,y2,圆角的角度
  244. class body(Shape): #身体
  245. def draw(self):
  246. self.qp.setPen(QPen(Qt.black, 2)) #边线
  247. self.qp.setBrush(self.color) #设置画刷
  248. self.qpp = QPainterPath() #路径
  249. self.qpp.moveTo(self.rect[0] + 228, self.rect[1] - 15)
  250. self.qpp.lineTo(self.rect[0] + 230 + 45, self.rect[1] + 25)
  251. self.qpp.lineTo(self.rect[0] + 230 + 20, self.rect[1] + 60)
  252. self.qpp.lineTo(self.rect[0] + 230 - 2, self.rect[1] + 40)
  253. self.qpp.lineTo(self.rect[0] + 230 - 2, self.rect[1] + 170)
  254. self.qpp.arcTo(self.rect[0] + 105,self.rect[1] + 170-10, 20, 20, 0, 180) #中间半圆
  255. self.qpp.lineTo(self.rect[0] -2, self.rect[1] + 170)
  256. self.qpp.lineTo(self.rect[0] - 2, self.rect[1] + 40)
  257. self.qpp.lineTo(self.rect[0] - 20, self.rect[1] + 60)
  258. self.qpp.lineTo(self.rect[0] - 40, self.rect[1] + 25)
  259. self.qpp.lineTo(self.rect[0] + 2, self.rect[1] - 14)
  260. self.qpp.closeSubpath()
  261. self.qp.drawPath(self.qpp)
  262. class foot(Shape): #脚
  263. def draw(self):
  264. linearGradient = QLinearGradient(140, 280, 140, 310)
  265. linearGradient.setColorAt(0.2, QColor("#c40")) #10%处
  266. linearGradient.setColorAt(1.0, QColor("#800400")) #
  267. # linearGradient.setColorAt(1.0, Qt.black) #100%处黑色
  268. self.qp.setPen(QPen(Qt.black, 2)) #边线
  269. # self.qp.setBrush(linearGradient) #设置画刷
  270. self.qpp = QPainterPath() #路径
  271. self.qpp.moveTo(self.rect[0] + self.rect[2] - 40, self.rect[1] + self.rect[3] - 2)
  272. self.qpp.arcTo(self.rect[0] + self.rect[2] - 40, self.rect[1] - 2 , 30.0, 30.0, 270.0, 180.0) #(70, 30, R*2, R*2, 0, 180);(70,30)是起始位置,R是半径,270是起始角度,180是要画的角度
  273. self.qpp.lineTo(self.rect[0], self.rect[1]-2)
  274. self.qpp.arcTo(self.rect[0]-15, self.rect[1] - 3, 60.0, 40.0, 90.0, 125.0)
  275. # self.qpp.arcTo(self.rect[0]-20, self.rect[1] + 26, 20.0, 20.0, 90.0, 30.0)
  276. self.qpp.closeSubpath()
  277. self.qp.drawPath(self.qpp)
  278. class hand(Shape): #手
  279. def draw(self):
  280. self.qp.setBrush(self.color) #设置画刷
  281. self.qp.drawEllipse(self.rect) #画圆形x,y,w,h 120, 25, 160, 160
  282. class chest(Shape): #白色肚兜
  283. def draw(self):
  284. self.color = Qt.white
  285. self.qp.setPen(QPen(Qt.black, 2)) #边线
  286. self.qp.setBrush(self.color) #设置画刷
  287. self.qpp = QPainterPath() #路径
  288. self.qpp.moveTo(self.rect[0] + 30 , self.rect[1] + 20)
  289. self.qpp.arcTo(self.rect[0] , self.rect[1] , 170.0, 170.0, 130.0, 280.0) #(70, 30, R*2, R*2, 0, 180);(70,30)是起始位置,R是半径,270是起始角度,180是要画的角度
  290. self.qpp.closeSubpath()
  291. self.qp.drawPath(self.qpp)
  292. class half(Shape): #口袋
  293. def draw(self):
  294. self.color = Qt.white
  295. self.qp.setPen(QPen(Qt.black, 2)) #边线
  296. self.qp.setBrush(self.color) #设置画刷
  297. self.qpp = QPainterPath() #路径
  298. self.qpp.moveTo(self.rect[0] , self.rect[1] + 10)
  299. self.qpp.arcTo(self.rect[0] , self.rect[1] - 55 , self.rect[2], self.rect[3], 180.0, 180.0) #(70, 30, R*2, R*2, 0, 180);(70,30)是起始位置,R是半径,270是起始角度,180是要画的角度
  300. self.qpp.closeSubpath()
  301. self.qp.drawPath(self.qpp)
  302. class bellc(Shape): #铃铛中心
  303. def draw(self):
  304. self.qp.setBrush(self.color) #设置画刷
  305. self.qp.drawEllipse(self.rect) #画圆形x,y,w,h 120, 25, 160, 160
  306. class bellv(Liner): #铃铛竖线
  307. def draw(self):
  308. self.qp.drawLine(self.rect)
  309. class bellh(Liner): #横线
  310. # def __init__(self, qp, start_point, w, c=0):
  311. # self.qp = qp #
  312. # self.start_point = start_point #起始坐标
  313. # self.w = w
  314. # self.c = c
  315. def draw(self):
  316. self.qp.drawLine(self.rect)
  317. class Bells: # 铃铛组合
  318. def __init__(self, qp, start_point, w, h): # w,h是铃铛的宽、高
  319. self.qp = qp #
  320. self.start_point = start_point #起始坐标
  321. self.w = w
  322. self.h = h
  323. self.be = bell(self.qp, self.be_cacu(), QColor("#a9a100")) #铃铛
  324. self.bec = bellc(self.qp, self.bec_cacu(),Qt.black) #铃铛中心
  325. self.belv = bellv(self.qp, self.belv_cacu()) #铃铛竖线
  326. self.belh1 = bellh(self.qp, self.belh1_cacu())
  327. self.belh2 = bellh(self.qp, self.belh2_cacu())
  328. def draw(self):
  329. self.be.draw()
  330. self.bec.draw()
  331. self.belv.draw()
  332. self.belh1.draw()
  333. self.belh2.draw()
  334. def belh1_cacu(self): # 计算铃铛横线的坐标
  335. x = self.start_point[0] + 2
  336. y = self.start_point[1] -3
  337. w = x + 36
  338. h = y
  339. return QLineF(x, y, w, h)
  340. def belh2_cacu(self): # 计算铃铛横线的坐标
  341. x = self.start_point[0]
  342. y = self.start_point[1] + 2
  343. w = x + 40
  344. h = y
  345. return QLineF(x, y, w, h)
  346. def belv_cacu(self): # 计算铃铛竖线的坐标
  347. x = self.start_point[0] + 20
  348. y = self.start_point[1] + 20
  349. w = x
  350. h = y + 8
  351. return QLineF(x, y, w, h)
  352. def bec_cacu(self): # 计算铃铛的坐标(圆角矩形)
  353. x = self.start_point[0] + self.w /2 - 5
  354. y = self.start_point[1] + 8
  355. w = 10
  356. h = 10
  357. return QRect(x, y, w, h)
  358. def be_cacu(self): # 计算铃铛的坐标(圆角矩形)
  359. x = self.start_point[0] + self.w /2 - 20
  360. y = self.start_point[1] - 12
  361. w = 40
  362. h = 40
  363. return QRect(x, y, w, h)
  364. class Bodys: # 身体组合
  365. def __init__(self, qp, start_point, w, h): # w,h是帽子的宽、高
  366. self.qp = qp #
  367. self.start_point = start_point #起始坐标
  368. self.w = w
  369. self.h = h
  370. self.bod = body(self.qp, self.bod_cacu()) #实例化身体
  371. self.che = chest(self.qp, self.che_cacu()) #实例化白色肚兜
  372. self.ha = half(self.qp, self.ha_cacu()) #实例化口袋
  373. self.coll = collar(self.qp, self.col_cacu()) # 实例化项圈
  374. self.hnd0 = hand(self.qp, self.hnd0_cacu(), Qt.white) #左手
  375. self.hnd1 = hand(self.qp, self.hnd1_cacu(), Qt.white) #左手
  376. self.ft1 = foot(self.qp, self.ft1_cacu(), Qt.white) #脚
  377. self.ft2 = foot(self.qp, self.ft2_cacu(), Qt.white) #脚
  378. self.bels = Bells(self.qp, (240, 300), 40, 40)
  379. def draw(self): # 绘制
  380. self.bod.draw() #调用项圈方法绘制
  381. self.che.draw()
  382. self.ha.draw()
  383. self.coll.draw()
  384. self.hnd0.draw()
  385. self.hnd1.draw()
  386. self.ft1.draw()
  387. self.ft2.draw()
  388. self.bels.draw()
  389. def ft1_cacu(self): # 计算脚的坐标
  390. x1 = self.start_point[0]
  391. y1 = self.start_point[1] + 170
  392. x2 = 120
  393. y2 = 30
  394. return (x1, y1, x2, y2)
  395. def ft2_cacu(self): # 计算脚的坐标(圆角矩形)
  396. x1 = self.start_point[0] + 130
  397. y1 = self.start_point[1] + 170
  398. x2 = 120
  399. y2 = 30
  400. return (x1, y1, x2, y2)
  401. def hnd0_cacu(self): # 计算手的坐标(圆角矩形)
  402. x = self.start_point[0] - 78
  403. y = self.start_point[1] + 25
  404. w = 60
  405. h = 60
  406. return QRect(x, y, w, h)
  407. def hnd1_cacu(self): # 计算手的坐标(圆角矩形)
  408. x = self.start_point[0] + self.h + 20
  409. y = self.start_point[1] + 25
  410. w = 60
  411. h = 60
  412. return QRect(x, y, w, h)
  413. def ha_cacu(self): # 计算口袋的坐标(圆角矩形)
  414. x = self.start_point[0] + 48
  415. y = self.start_point[1] + 40
  416. w = 130
  417. h = 130
  418. return (x, y, w, h)
  419. def che_cacu(self): # 计算白色肚兜的坐标(圆角矩形)
  420. x = self.start_point[0] + 28
  421. y = self.start_point[1] - 30
  422. w = 170
  423. h = 170
  424. return (x, y, w, h)
  425. def col_cacu(self): # 计算项圈的坐标(圆角矩形)
  426. x1 = self.start_point[0]
  427. y1 = self.start_point[1] - 20
  428. x2 = 230
  429. y2 = 20
  430. return (x1, y1, x2, y2)
  431. def bod_cacu(self): # 计算身体的坐标
  432. x = self.start_point[0]
  433. y = self.start_point[1]
  434. w = 230
  435. h = 165
  436. return (x, y, w, h)
  437. class Example(QWidget):
  438. def __init__(self, w, h):
  439. super().__init__()
  440. self.qp = QPainter() # 画笔实例
  441. self.rect = QRect()
  442. self.color = QColor()
  443. self.start_point = (100,30) #头部的起始位置
  444. self.w = w
  445. self.h = h
  446. self.initUI()
  447. self.heads = Heads(self.qp,self.start_point, 320, 300) # w,h
  448. self.bo = Bodys(self.qp, (145,300), 230, 230) #start_point,w,h
  449. def initUI(self):
  450. self.setGeometry(100, 300, self.w, self.h) # 窗口在屏幕上的坐标和大小:x,y,w,h
  451. self.setWindowTitle('叮当猫')
  452. self.show()
  453. # rect = QRect(120, 25, 160, 160)
  454. # self.hd = head(self.qp, self.rect, self.color)
  455. def paintEvent(self, e):
  456. self.qp.begin(self)
  457. self.drawBrushes(self.qp) #调用方法
  458. self.qp.end()
  459. def drawBrushes(self, qp):
  460. self.heads.draw()
  461. self.bo.draw()
  462. # brush.setStyle(Qt.Dense1Pattern) #设置style
  463. # qp.setBrush(brush) #设置画刷
  464. # self.qp.drawEllipse(self.rect, self.color) #画矩形x,y,w,h
  465. if __name__ == '__main__':
  466. app = QApplication(sys.argv)
  467. ex = Example(520, 760) #窗体大小
  468. sys.exit(app.exec_())

文章来源: zhulin1028.blog.csdn.net,作者:zhulin1028,版权归原作者所有,如需转载,请联系作者。

原文链接:zhulin1028.blog.csdn.net/article/details/120536768

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。