Skip to content

Commit 085d8f5

Browse files
committed
PyQT5 Comandos de Dibujo
Usar el evento PaintEvent para pintar texto, imágenes y figuras.
1 parent fb0d39d commit 085d8f5

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

04-dibujar/image/image.jpg

438 KB
Loading

04-dibujar/image/small_image.jpg

22.8 KB
Loading

04-dibujar/paint.py

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
__author__ = 'Carmelo Marin Abrego'
2+
3+
import sys
4+
5+
from PyQt5.QtGui import QPainter, QFont, QColor, QPixmap, QPen, QBrush
6+
from PyQt5.QtCore import Qt, QRect, QPoint
7+
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
8+
9+
10+
class Example(QWidget):
11+
def __init__(self):
12+
super().__init__()
13+
self.initUI()
14+
15+
def initUI(self):
16+
self.resize(1280, 720)
17+
self.setWindowTitle('PyQT5 Paint Event')
18+
19+
def paintEvent(self, event):
20+
qp = QPainter()
21+
qp.begin(self)
22+
self.drawImage(event, qp, 'image/image.jpg')
23+
self.drawText(event, qp, 'Tutor de Programación\nPintar con PyQT5')
24+
self.drawPoint(event, qp)
25+
self.drawLine(event, qp)
26+
self.drawShape(event, qp)
27+
qp.end()
28+
29+
def drawShape(self, event, qp):
30+
pen = QPen(Qt.yellow)
31+
pen.setWidth(3)
32+
# rellenar fondo con patron de imagen
33+
brush = QBrush()
34+
brush.setTexture(QPixmap('image/small_image.jpg'))
35+
# establecer el QBrush
36+
qp.setBrush(brush)
37+
qp.setPen(pen)
38+
qp.drawRoundedRect(QRect(50, 50, 200, 150), 15, 15)
39+
# utilizar un patron predefinido
40+
brush.setStyle(Qt.DiagCrossPattern)
41+
qp.setBrush(brush)
42+
qp.drawEllipse(350, 50, 150, 150)
43+
44+
def drawPoint(self, event, qp):
45+
pen = QPen(Qt.red)
46+
pen.setWidth(10)
47+
48+
qp.setPen(pen)
49+
qp.drawPoint(event.rect().width() / 2, event.rect().height() / 2)
50+
51+
def drawLine(self, event, qp):
52+
pen = QPen()
53+
pen.setWidth(3)
54+
pen.setColor(QColor(128, 250, 25, 255))
55+
pen.setStyle(Qt.DotLine)
56+
57+
qp.setPen(pen)
58+
qp.drawLine(QPoint(10, 10), QPoint(1280 - 10, 10))
59+
60+
color = QColor()
61+
color.setNamedColor('#c2h6b4')
62+
63+
pen.setColor(color)
64+
pen.setStyle(Qt.DashLine)
65+
66+
qp.setPen(pen)
67+
qp.drawLine(QPoint(10, 10 + 20), QPoint(1280 - 10, 10 + 20))
68+
69+
70+
def drawImage(self, event, qp, image):
71+
pixmap = QPixmap(image)
72+
qp.drawPixmap(event.rect(), pixmap)
73+
# qp.drawPixmap(QRect(0, 0, 1280, 720), pixmap)
74+
75+
def drawText(self, event, qp, text):
76+
qp.setPen(QColor(0, 255, 255))
77+
qp.setFont(QFont('Consolas', 48))
78+
qp.drawText(event.rect(), Qt.AlignCenter, text)
79+
80+
81+
if __name__ == '__main__':
82+
app = QApplication(sys.argv)
83+
win = Example()
84+
win.show()
85+
sys.exit(app.exec_())
86+
87+

0 commit comments

Comments
 (0)