Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PyQt5按钮单击动画_Python_User Interface_Pyqt_Pyqt5 - Fatal编程技术网

Python PyQt5按钮单击动画

Python PyQt5按钮单击动画,python,user-interface,pyqt,pyqt5,Python,User Interface,Pyqt,Pyqt5,早上好 问题1 在我的代码中,当你点击一个按钮时,它会闪烁几毫秒的红色 正如你所看到的,我有一个淡入淡出功能。淡入淡出功能如何知道它必须更改调用该功能的按钮的样式表 from PyQt5.QtCore import (Qt, QTimer) from PyQt5.QtWidgets import (QWidget, QLCDNumber, QSlider, QVBoxLayout, QApplication, QPushButton, QGridLayout, QHBoxLayout, QLab

早上好

问题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

更高级的是,我如何在按钮上制作类似于您单击手机锁屏时的动画


提前感谢。

首先,插槽可以调用
self.sender()
查找发出信号的对象

接下来,您必须注意信号负载,在本例中,它是可选的“checked True/False”,因此按钮不是fade()插槽的有效参数

还建议使用
pyqtSlot
装饰插槽,这没有缺点,即使它的优点现在不相关,以后也会相关(并且在邮件列表中公布,当装饰器未使用时,为您节省一些与信号处理相关的bug)。正如ehkumoro所指出的,由于checked是可选的,而您不使用它,因此使用pyqtSlot()进行装饰就足够了

最后,
undede()
需要知道要取消哪个按钮;由eyllanesc完成的lambda很好,但我将展示如何使用数据成员完成它,它的优点是它在
MainInterface
上扮演“状态标志”的角色。即,如果不是无,则表示
main接口
widget“当前正在淡出一个按钮”;当您的小部件响应在
fade()
结束和
undede()
开始之间发生的事件时,如再次单击(小部件需要测试是否已使另一个按钮褪色),这通常很方便


对于单击的信号(bool checked=false),可以使用
@pyqtSlot()
,这样可以消除死参数。
def __init__(self):
    ...
    self.__fading_button = None

@pyqtSlot()
def fade(self):
    self.__fading_button = self.sender()  # enter the "fading button" state
    self.__fading_button.setWindowOpacity(0.5)
    self.__fading_button.setStyleSheet("background-color: red")
    QTimer.singleShot(300, self.unfade)

def unfade(self):
    self.__fading_button.setWindowOpacity(1)
    self.__fading_button.setStyleSheet("background-color: #2e3436")
    self.__fading_button = None  # exit the "fading button" state