QLineEdit边框闪烁

源码

from PyQt5 import QtCore
from PyQt5.QtCore import QPropertyAnimation, pyqtProperty
from PyQt5.QtWidgets import QMainWindow, QApplication, QLineEdit
from PyQt5 import QtWidgets


class NewLineEdit(QLineEdit):
    def __init__(self, parent=None):
        super(NewLineEdit, self).__init__(parent)

    def _set_color(self, value):
        color = 'border: 1px solid rgba(255, 0, 0, %s);' % value
        self.setStyleSheet(color)

    color = pyqtProperty(int, fset=_set_color)


class Form(QMainWindow):
    def __init__(self):
        super(Form, self).__init__()
        self.setupUi()

        self.pushButton.clicked.connect(self.pushButton_clicked)

    def pushButton_clicked(self):
        self.animation = QPropertyAnimation(self.lineEdit, b'color')
        self.animation.setDuration(200)
        self.animation.setLoopCount(3)
        self.animation.setStartValue(255)
        self.animation.setKeyValueAt(0.5, 0)
        self.animation.setEndValue(255)
        self.animation.start()

    def setupUi(self):
        self.resize(400, 300)
        self.centralwidget = QtWidgets.QWidget(self)
        self.lineEdit = NewLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(160, 100, 113, 20))
        self.pushButton = QtWidgets.QPushButton('pushbutton', self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(50, 100, 75, 23))
        self.setCentralWidget(self.centralwidget)


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    form = Form()
    form.show()
    app.exec_()

 截图

QLineEdit边框闪烁_第1张图片

你可能感兴趣的:(PyQt5)