我想用pyqt5为按钮创建一个彩色动画

2024-09-29 23:19:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用pyqt5QpushButton创建了一个虚拟按钮键盘,并在QLineEdit字符长度满足时激活ok按钮。我想创建一个彩色动画效果(美丽的绘制)当按钮被激活。使用QPropertyAnimation我不知道你是否认为我做了QT手册。在

import sys
from functools import partial
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.center_widget = QStackedWidget()
        self.setCentralWidget(self.center_widget)
        self.SearchUI()
    def SearchUI(self):
        widget = QWidget()
        mainlayout = QVBoxLayout(widget)
        button_gruop = QGroupBox()
        gridlayout = QGridLayout()

        count = 0
        self.numberSave = ''
        self.buttons = []
        self.search = QLineEdit('')
        self.search.setAlignment(Qt.AlignCenter)
        self.search.setStyleSheet('font: bold 50pt')
        self.search.setMaxLength(13)
        self.search.setEchoMode(QLineEdit.Password)
        virtualkeypad = [
            '7','8','9',
            '4','5','6',
            '1','2','3',
            'BACK','0','OK'
        ]
        positions = [(i, j) for i in range(4) for j in range(3)]
        for position, name in zip(positions, virtualkeypad):
            self.buttons.append(QPushButton(name))
            gridlayout.addWidget(self.buttons[count], *position)
            self.buttons[count].setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
            count += 1

        for i in [0,1,2,3,4,5,6,7,8,10]:
            self.buttons[i].clicked.connect(partial(self.ButtonNumberSelect, '{}'.format(virtualkeypad[i])))
            self.buttons[i].setStyleSheet('background-color: orange; font: bold 50pt;')

        self.buttons[9].clicked.connect(self.ButtonBackSpace)
        self.buttons[11].clicked.connect(self.ButtonEnter)
        self.buttons[9].setStyleSheet('background-color: orange; font: 50pt;')
        self.buttons[11].setStyleSheet('background-color: none; font: 50pt;')
        self.buttons[11].setEnabled(False)

        button_gruop.setLayout(gridlayout)
        mainlayout.addWidget(self.search)
        mainlayout.addWidget(button_gruop)
        mainlayout.setContentsMargins(150,150,150,150)
        mainlayout.contentsMargins()
        self.center_widget.addWidget(widget)

    def ButtonNumberSelect(self, button):
        self.numberSave += button
        self.search.setText(self.numberSave)
        if len(self.numberSave) == 13:
            a = QGraphicsColorizeEffect(self.buttons[11])
            self.buttons[11].setGraphicsEffect(a)
            animation_button = QPropertyAnimation(a)
            animation_button.setStartValue(QColor(Qt.cyan))
            animation_button.setKeyValueAt(0.10(Qt.darkCyan))
            animation_button.setKeyValueAt(0.10(Qt.darkBlue))
            animation_button.setKeyValueAt(0.10(Qt.darkGray))
            animation_button.setKeyValueAt(0.10(Qt.darkGreen))
            animation_button.setKeyValueAt(0.10(Qt.darkMagenta))
            animation_button.setKeyValueAt(0.10(Qt.darkYellow))
            animation_button.setEndValue(QColor(255,255,255))
            animation_button.setDuration(5000)
            animation_button.setLoopCount(5)
            animation_button.start()
            self.buttons[11].setEnabled(True)
            self.buttons[11].setStyleSheet('background-color: orange; font: 50pt;')
            self.numberSave = ''
        else:
            self.buttons[11].setEnabled(False)
            self.buttons[11].setStyleSheet('background-color: white; font: 50pt;')

    def ButtonBackSpace(self):
        self.numberSave = self.numberSave[:-1]
        self.search.setText(self.numberSave)

    def ButtonEnter(self):
        self.center_widget.setCurrentIndex(0)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    fream = MainWindow()
    fream.show()
    app.exec_()

=添加编辑= 当我运行代码并用数字按钮生成13个数字时,出现了一个错误。在

^{pr2}$

def ButtonNumberSelect (self, button):部件似乎有问题。在

a = QGraphicsColorizeEffect (self.buttons [11])
self.buttons [11] .setGraphicsEffect (a)
animation_button = QPropertyAnimation (a)
animation_button.setStartValue (QColor (Qt.darkBlue))
animation_button.setEndValue (QColor (255,255,255))
animation_button.setDuration (5000)
animation_button.setLoopCount (5)
animation_button.start ()

Tags: importselfsearchdefbuttonwidgetqtbackground
1条回答
网友
1楼 · 发布于 2024-09-29 23:19:28

正如我在评论中指出的,我仍然不知道你想写什么:

animation_button.setKeyValueAt(0.10(Qt.XXX))

更合适的方法是:

^{pr2}$

但是把所有这些都放在0.1%是没有意义的。在

另一方面,QPropertyAnimation不告诉您要修改什么属性,假设您要更改颜色,应该类似于:

animation_button = QPropertyAnimation(a, b"color")

通过重新排列和清理代码,可以得到以下结果:

from PyQt5 import QtCore, QtGui, QtWidgets
from functools import partial


class BeautifulButton(QtWidgets.QPushButton):
    def __init__(self, *args, **kwargs):
        super(BeautifulButton, self).__init__(*args, **kwargs)
        effect = QtWidgets.QGraphicsColorizeEffect(self)
        self.setGraphicsEffect(effect)

        self.animation = QtCore.QPropertyAnimation(effect, b"color")

        self.animation.setStartValue(QtGui.QColor(QtCore.Qt.cyan))
        self.animation.setEndValue(QtGui.QColor(255,255,255))

        self.animation.setLoopCount(5)
        self.animation.setDuration(5000)


class Page(QtWidgets.QWidget):
    okClicked = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        super(Page, self).__init__(parent)
        mainlayout = QtWidgets.QVBoxLayout(self)
        self.keypad = QtWidgets.QGroupBox()
        self.search = QtWidgets.QLineEdit()
        self.search.setProperty("last_text", "")
        self.search.setAlignment(QtCore.Qt.AlignCenter)
        self.search.setStyleSheet('font: bold 50pt')
        self.search.setMaxLength(13)
        self.search.setEchoMode(QtWidgets.QLineEdit.Password)
        mainlayout.addWidget(self.search)
        mainlayout.addWidget(self.keypad)
        mainlayout.setContentsMargins(150,150,150,150)

        lay = QtWidgets.QGridLayout(self.keypad)

        virtualkeypad = [
            '7','8','9',
            '4','5','6',
            '1','2','3',
            'BACK','0','OK'
        ]
        positions = [(i, j) for i in range(4) for j in range(3)]

        self.buttons = {}

        for position, name in zip(positions, virtualkeypad):
            if name == "OK":
                btn = BeautifulButton(name)
                btn.setStyleSheet('background-color: none; font: 50pt;')
                btn.setDisabled(True)
            else:
                btn = QtWidgets.QPushButton(name)
                btn.setStyleSheet('background-color: orange; font: bold 50pt;')

            self.buttons[name] = btn
            btn.clicked.connect(partial(self.on_clicked, name))
            btn.setSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Expanding)
            lay.addWidget(btn, *position)

    def on_clicked(self, text):
        if text in  map(str, range(0, 10)):
            if len(self.search.text()) == 13:
                self.search.clear()
            self.search.insert(text)
            btn = self.buttons["OK"]
            if len(self.search.text()) == 13:
                btn.setEnabled(True)
                btn.setStyleSheet('background-color: orange; font: bold 50pt;')
                btn.animation.start()
            else:
                btn.setEnabled(False)
                btn.setStyleSheet('background-color: white; font: 50pt;')
                btn.animation.stop()
        elif text == "BACK":
            self.search.backspace()
        elif text == "OK":
            self.okClicked.emit()


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.center_widget = QtWidgets.QStackedWidget()
        self.setCentralWidget(self.center_widget)
        self.SearchUI()

    def SearchUI(self):
        page = Page()
        page.okClicked.connect(partial(self.center_widget.setCurrentIndex, 0))
        self.center_widget.addWidget(page)


if __name__ == '__main__':
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

相关问题 更多 >

    热门问题