python - 如何在帧中生成动画

标签 python python-3.x pyqt pyqt5

在图像中,我显示了一个程序,其中我尝试在鼠标位于主橙色框架上时显示一个框架。 不过,边框的出现却十分突兀。 我想知道我们是否可以创建一种动画,这样框架就不会以这种方式出现,而是向右滑动

enter image description here

代码:

from PyQt5.QtWidgets import QMainWindow,QApplication
from PyQt5 import uic 
from PyQt5 import Qt
from PyQt5 import QtCore

class Principal(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        uic.loadUi("we.ui",self)

        self.setAttribute(Qt.Qt.WA_TranslucentBackground, True )   
        self.setAttribute(Qt.Qt.WA_NoSystemBackground, False)      
        self.setWindowFlags(Qt.Qt.FramelessWindowHint)
        self.frame1.installEventFilter(self)

    def eventFilter(self,watched,event):
        if self.frame1 is watched:
            if event.type() == QtCore.QEvent.Enter:
                self.frame2.resize(121,self.height())
            elif event.type() == QtCore.QEvent.Leave:
                self.frame2.resize(10,10)
        return super(Principal,self).eventFilter(watched,event)

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

文件.ui:

<?xml version="1.0" encoding="UTF-8"?>
      <ui version="4.0">
       <class>MainWindow</class>
       <widget class="QMainWindow" name="MainWindow">
        <property name="geometry">
         <rect>
          <x>0</x>
          <y>0</y>
          <width>552</width>
          <height>397</height>
         </rect>
        </property>
        <property name="windowTitle">
         <string>MainWindow</string>
        </property>
        <property name="styleSheet">
         <string notr="true">background:qlineargradient(spread:pad, x1:0.565, y1:0, x2:0.508475, y2:1, stop:0 rgba(0, 0, 103, 0), stop:1 rgba(255, 255, 255, 0));</string>
        </property>
        <widget class="QWidget" name="centralwidget">
         <property name="styleSheet">
          <string notr="true"/>
         </property>
         <widget class="QPushButton" name="Ajustes">
          <property name="geometry">
           <rect>
            <x>406</x>
            <y>0</y>
            <width>31</width>
            <height>23</height>
           </rect>
          </property>
          <property name="styleSheet">
           <string notr="true">QPushButton#Ajustes{
      background:none;
      border:0px;
      }</string>
          </property>
          <property name="text">
           <string/>
          </property>
          <property name="icon">
           <iconset>
            <normaloff>settings.png</normaloff>settings.png</iconset>
          </property>
         </widget>
         <widget class="QFrame" name="frame1">
          <property name="geometry">
           <rect>
            <x>0</x>
            <y>0</y>
            <width>431</width>
            <height>401</height>
           </rect>
          </property>
          <property name="styleSheet">
           <string notr="true">background:orange;</string>
          </property>
          <property name="frameShape">
           <enum>QFrame::StyledPanel</enum>
          </property>
          <property name="frameShadow">
           <enum>QFrame::Raised</enum>
          </property>
         </widget>
         <widget class="QFrame" name="frame2">
          <property name="geometry">
           <rect>
            <x>430</x>
            <y>0</y>
            <width>0</width>
            <height>401</height>
           </rect>
          </property>
          <property name="styleSheet">
           <string notr="true">background:red;</string>
          </property>
          <property name="frameShape">
           <enum>QFrame::StyledPanel</enum>
          </property>
          <property name="frameShadow">
           <enum>QFrame::Raised</enum>
          </property>
         </widget>
         <zorder>frame1</zorder>
         <zorder>Ajustes</zorder>
         <zorder>frame2</zorder>
        </widget>
       </widget>
       <resources/>
       <connections/>
      </ui>

我希望你能帮助我,我已经寻找了相关的东西,但我找不到如何制作这样的动画,如果你可以这样调用它们

最佳答案

您必须使用QPropertyAnimation :

from PyQt5 import QtCore, QtWidgets, uic 

class Principal(QtWidgets.QMainWindow):
    def __init__(self):
        super(Principal, self).__init__()
        uic.loadUi("we.ui",self)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)   
        self.setAttribute(QtCore.Qt.WA_NoSystemBackground, False)      
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        self.frame1.installEventFilter(self)
        self._animation = QtCore.QPropertyAnimation(self.frame2, b'size', self)
        self._animation.setStartValue(QtCore.QSize(0, self.height()))
        self._animation.setEndValue(QtCore.QSize(121, self.height()))
        self._animation.setDuration(200)

    def eventFilter(self, watched, event):
        if self.frame1 is watched:
            if event.type() == QtCore.QEvent.Enter:
                self._animation.setDirection(QtCore.QAbstractAnimation.Forward)
                self._animation.start()
            elif event.type() == QtCore.QEvent.Leave:
                self._animation.setDirection(QtCore.QAbstractAnimation.Backward)
                self._animation.start()
        return super(Principal,self).eventFilter(watched, event)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    p = Principal()
    p.show()
    sys.exit(app.exec_())

关于python - 如何在帧中生成动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54048407/

相关文章:

python - 具有自动补全功能的 QComboBox 在 PyQt4 中有效,但在 PySide 中无效

python - 将 python 列表子集化为正/负运动/趋势

python - 在不使用表单或模型的情况下验证 django 中的单个字段

python-3.x - 使用 python 从 netcdf 绘制风向量

python - 如何阻止我的进程空闲或被杀死?

python - PyQt:十六进制的 QLineEdit 输入掩码

Python - 将按钮动态添加到 PyQt 中的布局

python - Flask 服务器启动后运行函数

python - pandas 中的连胜数 ID

Python 3.3 for() 循环迭代处理时间呈指数增长?