PyQt5 制作验证码
import random
from PyQt5.QtWidgets import QWidget
from PyQt5.QtGui import QPainter, QColor, QFont, QPixmap, QPolygon, QPen
from PyQt5.QtCore import Qt, QRect, QPoint
from utils import runtime_global_storage
class Example(QWidget):
def __init__(self, pic_width, v_code_width, v_code_height, multiple):
'''
:param pic_width: 显示图片的宽度
:param v_code_width: 画布宽度
:param v_code_height: 画布高度
:param multiple: 验证码间隔
'''
super().__init__()
self.initUI()
self.pic_width = pic_width
self.v_code_width = v_code_width
self.v_code_height = v_code_height
self.multiple = multiple
def initUI(self):
self.setGeometry(0, 0, 380, 53)
self.setWindowTitle("Draw text")
self.show()
runtime_global_storage.v_code_class = self
def paintEvent(self, event):
qp = QPainter()
qp.translate(0, 0)
qp.begin(self)
self.drawText(qp)
qp.end()
def drawText(self, qp):
self.text = ""
text_str = "qwertyuiopasdfghjklzxcvbnm1234567890"
images_list = ["b1", "b2", "b3", "b4", "b5"]
for i in range(6):
random_index = random.randint(0, 34)
self.text += text_str[random_index]
runtime_global_storage.v_code = self.text
qp.setRenderHint(QPainter.Antialiasing)
# gradient = QLinearGradient(0, 0, 0, 100)
# gradient.setColorAt(0.0, QColor(random.randint(50, 155), random.randint(50, 155), random.randint(50, 155)))
# gradient.setColorAt(1.0, QColor(random.randint(155, 255), random.randint(155, 255), random.randint(155, 255)))
# qp.setBrush(gradient)
qp.setFont(QFont("Decorative", 26, 75))
qp.drawPixmap(self.pic_width, 0, 250, 53, QPixmap("images/" + images_list[random.randint(0, 4)] + ".jpg"))
for i in range(len(self.text)):
qp.setPen(QColor(random.randint(50, 255), random.randint(50, 255), random.randint(50, 255)))
word_point = random.randint(-70 + (i * self.multiple), -50 + (i * self.multiple))
qp.rotate(0)
qp.rotate(random.uniform(-1.5, 1.5))
qp.drawText(QRect(word_point, 2, self.v_code_width, self.v_code_height), Qt.AlignCenter, self.text[i])
qp.setPen(QPen(QColor(random.randint(50, 255), random.randint(50, 255), random.randint(50, 255)), 2, 1, 0, 0))
for i in range(2):
# random_width = random.randint(self.pic_width + 6, self.pic_width + 244)
# random_height = random.randint(6, 48)
# points1 = [QPoint(random_width, random_height),
# QPoint(random_width - 6, random_height)]
# points2 = [QPoint(random_width - 3, random_height),
# QPoint(random_width - 3, random_height + 2), QPoint(random_width - 3, random_height - 4)]
# points1 = [QPoint(random_width, random_height), QPoint(random_width - 6, random_height)]
# points2 = [QPoint(random_width, random_height), QPoint(random_width - 6, random_height)]
# qp.drawPolyline(QPolygon(points1))
# qp.drawPolyline(QPolygon(points2))
# qp.setPen(QColor(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
points = [
QPoint(random.randint(self.pic_width + 6, self.pic_width + 244), random.randint(6, 48)),
QPoint(random.randint(self.pic_width + 6, self.pic_width + 244), random.randint(6, 48)),
QPoint(random.randint(self.pic_width + 6, self.pic_width + 244), random.randint(6, 48)),
]
qp.drawPolygon(QPolygon(points))
def mousePressEvent(self, QMouseEvent):
self.repaint()
效果图: