【发布时间】:2017-05-18 11:31:43
【问题描述】:
早上好。
问题 1 在我的代码中,我试图让它当你点击一个按钮时,它会闪烁红色几毫秒。
如你所见,我有一个淡入淡出和不淡出的功能。淡入淡出函数如何知道它必须更改调用该函数的按钮的样式表?
from PyQt5.QtCore import (Qt, QTimer)
from PyQt5.QtWidgets import (QWidget, QLCDNumber, QSlider, QVBoxLayout, QApplication, QPushButton, QGridLayout, QHBoxLayout, QLabel)
from PyQt5.QtGui import (QFont, QPainter, QColor)
# Font declaration
mainFont = QFont("arial", 18, QFont.Bold)
# Color declaration
"""
backgroundColor = "#2e3436"
buttonColor = "#729fcf"
textColor = "#2e3436"
greenColor = "#27bc10"
redColor = "#d81711"
"""
backgroundColor = "#2e3436"
buttonColor = "#ffffff"
textColor = "#000000"
greenColor = "#27bc10"
redColor = "#d81711"
# main buttons size
mainWidth = 160
mainHeight = 80
# Variable declaration
CurrentSpeed = 1
class MainInterface(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
b1 = QPushButton('Start')
b1.setFixedWidth(mainWidth)
b1.setFixedHeight(mainHeight)
b1.setStyleSheet("background-color: %s; color: %s" % (greenColor, textColor))
b1.setFont(mainFont)
b1.clicked.connect(self.fade)
b2 = QPushButton('Stop')
b2.setFixedWidth(mainWidth)
b2.setFixedHeight(mainHeight)
b2.setStyleSheet("background-color: %s; color: %s" % (redColor, textColor))
b2.setFont(mainFont)
b3 = QPushButton('Speed -')
b3.setFixedWidth(mainWidth)
b3.setFixedHeight(mainHeight)
b3.setStyleSheet("background-color: %s; color: %s" % (buttonColor, textColor))
b3.setFont(mainFont)
l1 = QLabel(str(CurrentSpeed))
l1.setStyleSheet("color: white; margin: 15px; border: 2px solid white")
l1.setFont(mainFont)
l1.setAlignment(Qt.AlignCenter)
b4 = QPushButton('Speed +')
b4.setFixedWidth(mainWidth)
b4.setFixedHeight(mainHeight)
b4.setStyleSheet("background-color: %s; color: %s" % (buttonColor, textColor))
b4.setFont(mainFont)
grid = QGridLayout()
grid.setColumnMinimumWidth(50, 400)
grid.setRowMinimumHeight(10, 250)
grid.addWidget(b1, 0, 0)
grid.addWidget(b2, 0, 2)
grid.addWidget(b3, 1, 0)
grid.addWidget(l1, 1, 1)
grid.addWidget(b4, 1, 2)
self.setStyleSheet("background-color: %s" % backgroundColor)
self.setLayout(grid)
self.setGeometry(300, 200, 850, 450)
self.setWindowTitle('Signal & slot')
self.show()
def fade(self, button):
button.setWindowOpacity(0.5)
button.setStyleSheet("background-color: red")
QTimer.singleShot(300, self.unfade)
def unfade(self):
button.setWindowOpacity(1)
button.setStyleSheet("background-color: #2e3436")
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
ex = MainInterface()
sys.exit(app.exec_())
问题 2
更高级,如何在按钮上制作类似于点击手机锁屏时的动画?
提前致谢。
【问题讨论】:
标签: python user-interface pyqt pyqt5