Home > Enterprise >  PyQt5 PushButton gradient animation
PyQt5 PushButton gradient animation

Time:02-21

I am trying to make an animated transition for a QPushButton.

This is the Stylesheet which the button currently has.

QPushButton{
border-radius:5px;
background-color: rgb(49,  56,  68);
}

I want to make an animation which goes from the first stylesheet to the second one:

QPushButton{
border-radius:5px;
background-color: qlineargradient(x1: 0, y1: 0.2, x2: 1, y2: 1,
                    stop: 0 rgba(53, 84, 232,150), stop: 1 rgba(73, 104, 252,255));
}

in a smooth way but i haven't found any solution to do this.

CodePudding user response:

Gradient color codes don't work here. I think you can use gradient background images.

CodePudding user response:

Made something similair to what i wanted. Doesnt fully work but thats just the first piece of code i wrote.

        self.timer1 = QTimer()
        self.timer1.setInterval(4)


    def transitionthread(self, button):
        global r,b,g,r2,g2,b2,rd,gd,bd,rd2,gd2,bd2

        if(r != 53):r  = 1
        else:
            rd = True
            pass
        if(g != 84):g  = 1
        else:
            gd = True
            pass
        if(b != 232):b  = 1
        else:
            bd = True
            pass

        if(r2 != 73):r2  = 1
        else:
            rd2 = True
            pass
        if(g2 != 104):g2  = 1
        else:
            gd2 = True
            pass
        if(b2 != 252):b2  = 1
        else:
            bd2 = True
            pass

        button.setStyleSheet("""
QPushButton {
    color: rgb(255, 255, 255);
background-color: qlineargradient(x1: 0, y1: 0.2, x2: 1, y2: 1,
                    stop: 0 rgba(%s, %s, %s,150), stop: 1 rgba(%s, %s, %s,255));
}
"""%(r,g,b,r2,g2,b2))                                              #73, 104, 252, 255 is nich wie links

        if rd==True and gd== True and bd== True and rd2== True and gd2== True and bd2== True:
            print("Finished")
            QtCore.QTimer.singleShot(100, self.resetData)
            self.timer1.stop()

    def activate(self):
        sending_button = self.sender()
        if sending_button.objectName() == "pushButton_3":
            Module1 = True
            print(Module1)
        self.timer1.timeout.connect(lambda: self.transitionthread(button = sending_button))
        self.timer1.start()
    def resetData(self):
        rd,gd,bd,rd2,gd2,bd2 = False, False, False, False, False, False
        r = 49
        b = 68
        g = 56

        r2 = 49
        b2 = 68
        g2 = 56
        print("done")
  • Related