【问题标题】:how to generate an animation in a frame如何在帧中生成动画
【发布时间】:2019-01-05 02:15:49
【问题描述】:

在图像中,我展示了一个程序,在该程序中,当鼠标位于我的橙色主框架上时,我尝试显示一个框架。 但是,框架的出现非常突兀。 我想知道我们是否可以创建一种动画,而不是以那种方式出现,看起来框架向右滑动

代码:

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>

希望您能帮助我

【问题讨论】:

    标签: python python-3.x pyqt pyqt5


    【解决方案1】:

    您必须使用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_())
    

    【讨论】:

    • 这一行self._animation = QtCore.QPropertyAnimation(self.frame2, b'size', self)是什么:b'size'
    • @Mystic_Force QPropertyAnimation 用于为 qproperty 设置动画,在这种情况下,qproperty 是大小,即 b'size'
    • 而在eventFillter函数的watched参数的情况下,`watched`来自哪里
    猜你喜欢
    • 2013-07-16
    • 2021-01-19
    • 2020-01-27
    • 2021-11-01
    • 2018-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多