Start animation with hover event in python

2

I'm doing an animation of a simple frame, but I want the animation to start when the cursor is on the frame and return to its initial shape when it leaves the frame.

Since at the moment it only works with one button, this is my code

import sys
from PyQt5.QtWidgets import QMainWindow,QApplication,QPushButton,QFrame
from PyQt5.QtCore import QRect, QPropertyAnimation
from PyQt5 import uic

class Principal(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)


        self.boton = QPushButton("animacion",self)
        self.boton.clicked.connect(self.anima)

        self.frame = QFrame(self)
        self.frame.setFrameStyle(QFrame.Panel | QFrame.Raised)
        self.frame.setGeometry(100,100,30,50)

    def bot(self):
        print("click")

    def anima(self):
        self.anim = QPropertyAnimation(self.frame,b"geometry")
        self.anim.setDuration(100)
        self.anim.setStartValue(QRect(100,100,30,50))
        self.anim.setEndValue(QRect(100,100,90,50))
        self.anim.start() 


app = QApplication([])
p = Principal()
p.resize(300,300)
p.show()
app.exec_()

and they could explain to me b "geometry" that other values can be placed instead of this in the animation

    
asked by Revsky01 26.06.2018 в 18:48
source

1 answer

1

Create a class that inherits from QFrame and reimplements QWidget.enterEvent and QWidget.leaveEvent in it:

import sys
from PyQt5.QtWidgets import QMainWindow,QApplication,QPushButton,QFrame
from PyQt5.QtCore import QRect, QPropertyAnimation
from PyQt5 import uic

class MyFrame(QFrame):

     def enterEvent(self, QEvent):
        anim = QPropertyAnimation(self, b"geometry")
        anim.setDuration(100)
        anim.setStartValue(QRect(100,100,90,50))
        anim.setEndValue(QRect(100,100,30,50))
        anim.start() 


     def leaveEvent(self, QEvent):
        anim = QPropertyAnimation(self, b"geometry")
        anim.setDuration(100)
        anim.setStartValue(QRect(100,100,30,50))
        anim.setEndValue(QRect(100,100,90,50))
        anim.start() 



class Principal(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.frame = MyFrame(self)
        self.frame.setFrameStyle(QFrame.Panel | QFrame.Raised)
        self.frame.setGeometry(100, 100, 30, 50)



app = QApplication([])
p = Principal()
p.resize(300,300)
p.show()
app.exec_()

You can also generate your own signal:

import sys
from PyQt5.QtWidgets import QMainWindow,QApplication,QPushButton,QFrame
from PyQt5.QtCore import QRect, QPropertyAnimation, pyqtSignal
from PyQt5 import uic

class MyFrame(QFrame):    
    mouseHover = pyqtSignal(bool)

    def enterEvent(self, event):
        self.mouseHover.emit(True)

    def leaveEvent(self, event):
        self.mouseHover.emit(False)



class Principal(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.frame = MyFrame(self)
        self.frame.setFrameStyle(QFrame.Panel | QFrame.Raised)
        self.frame.setGeometry(100, 100, 30, 50)
        self.frame.mouseHover.connect(self.hover_anim)


    def hover_anim(self, hovering):
        anim = QPropertyAnimation(self.frame, b"geometry")
        anim.setDuration(100)
        if hovering:
            anim.setStartValue(QRect(100,100,90,50))
            anim.setEndValue(QRect(100,100,30,50))
        else:
            anim.setStartValue(QRect(100,100,30,50))
            anim.setEndValue(QRect(100,100,30,50))
        anim.start() 



app = QApplication([])
p = Principal()
p.resize(300,300)
p.show()
app.exec_()

As soon as "geometry" refers to the property of the widget target of the animation. You have the possibility to create your own properties ( pyqtProperty ) in the subclass of your widget to implement other animations, such as changing the color.

    
answered by 26.06.2018 / 20:11
source