我使用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 ()
正如我在评论中指出的,我仍然不知道你想写什么:
更合适的方法是:
^{pr2}$但是把所有这些都放在0.1%是没有意义的。在
另一方面,
QPropertyAnimation
不告诉您要修改什么属性,假设您要更改颜色,应该类似于:通过重新排列和清理代码,可以得到以下结果:
相关问题 更多 >
编程相关推荐