为什么QPropertyAnimation动画不起作用?

时间:2019-01-08 02:15:38

标签: python python-3.x pyqt pyqt5 qpropertyanimation

当我按下按钮时,我试图生成动画,但是在self.frame2返回到大小0之后,它不起作用:

这是一个例子:

enter image description here

帧返回0后,动画不再执行:

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


class Login(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        uic.loadUi("1.-Login.ui",self)

        #Apariencia de Ventana
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        self.setAttribute(QtCore.Qt.WA_NoSystemBackground,True)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)

        #Botones
        self.Ajustes.clicked.connect(self.animaAjustes)


        self.ComboSuc.lineEdit().setAlignment(QtCore.Qt.AlignCenter)



        self.animation = QtCore.QPropertyAnimation(self.frame2, b'size', self)
        self.animation.setStartValue(QtCore.QSize(0,0))
        self.animation.setEndValue(QtCore.QSize(145,443))
        self.animation.setDuration(200)
        self.animation.setDirection(QtCore.QAbstractAnimation.Forward)




    def animaAjustes(self):

        if self.frame2.width()!=0:
            self.frame2.setGeometry(0,0,0,0)

        else:

            self.animation.start()



app = QApplication([])
l = Login()
l.show()
app.exec_()

file.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>606</width>
    <height>430</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">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="QFrame" name="frame1">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>-10</y>
      <width>481</width>
      <height>441</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">
QFrame#frame1{
background:White;
border:0px;

}</string>
    </property>
    <property name="frameShape">
     <enum>QFrame::StyledPanel</enum>
    </property>
    <property name="frameShadow">
     <enum>QFrame::Raised</enum>
    </property>
    <widget class="QFrame" name="frame3">
     <property name="geometry">
      <rect>
       <x>120</x>
       <y>50</y>
       <width>250</width>
       <height>120</height>
      </rect>
     </property>
     <property name="styleSheet">
      <string notr="true">background-image:url(src/Logo.png);
background-position:center;
background-repeat:none;</string>
     </property>
     <property name="frameShape">
      <enum>QFrame::StyledPanel</enum>
     </property>
     <property name="frameShadow">
      <enum>QFrame::Raised</enum>
     </property>
    </widget>
    <widget class="QPushButton" name="Ajustes">
     <property name="geometry">
      <rect>
       <x>450</x>
       <y>35</y>
       <width>31</width>
       <height>23</height>
      </rect>
     </property>
     <property name="styleSheet">
      <string notr="true">QPushButton#Ajustes{
border:0px;
background:none;
}</string>
     </property>
     <property name="text">
      <string/>
     </property>
     <property name="icon">
      <iconset>
       <normaloff>src/Icons/icons8-settings.png</normaloff>src/Icons/icons8-settings.png</iconset>
     </property>
    </widget>
    <widget class="QPushButton" name="Entrar">
     <property name="geometry">
      <rect>
       <x>200</x>
       <y>260</y>
       <width>81</width>
       <height>23</height>
      </rect>
     </property>
     <property name="styleSheet">
      <string notr="true">QPushButton#Entrar{
background:qlineargradient(spread:pad, x1:0.514, y1:0, x2:0.508475, y2:1, stop:0 rgba(0, 170, 213, 255), stop:1 rgba(19, 12, 64, 255));
color:white;
}
QPushButton#Entrar:pressed{
background:qlineargradient(spread:pad, x1:0.514, y1:0, x2:0.508475, y2:1, stop:0 rgba(19, 12, 64, 255), stop:0.994318 rgba(0, 170, 213, 255));
}

</string>
     </property>
     <property name="text">
      <string>Entrar</string>
     </property>
    </widget>
    <widget class="QLabel" name="LabelSuc">
     <property name="geometry">
      <rect>
       <x>190</x>
       <y>220</y>
       <width>101</width>
       <height>20</height>
      </rect>
     </property>
     <property name="font">
      <font>
       <pointsize>9</pointsize>
       <weight>50</weight>
       <bold>false</bold>
      </font>
     </property>
     <property name="styleSheet">
      <string notr="true">QLabel#LabelSuc{
border-bottom:1px solid qlineargradient(spread:pad, x1:0.983051, y1:0.608, x2:0, y2:0.585, stop:0 rgba(255, 255, 255, 0), stop:0.497175 rgba(16, 102, 214, 121), stop:1 rgba(255, 255, 255, 0));
color:#1687A1
}</string>
     </property>
     <property name="text">
      <string>Sucusal</string>
     </property>
     <property name="alignment">
      <set>Qt::AlignCenter</set>
     </property>
    </widget>
    <widget class="QFrame" name="frame">
     <property name="geometry">
      <rect>
       <x>0</x>
       <y>10</y>
       <width>481</width>
       <height>21</height>
      </rect>
     </property>
     <property name="styleSheet">
      <string notr="true">background:#0584B1;</string>
     </property>
     <property name="frameShape">
      <enum>QFrame::StyledPanel</enum>
     </property>
     <property name="frameShadow">
      <enum>QFrame::Raised</enum>
     </property>
     <widget class="QPushButton" name="BotonClose">
      <property name="geometry">
       <rect>
        <x>456</x>
        <y>-2</y>
        <width>21</width>
        <height>21</height>
       </rect>
      </property>
      <property name="font">
       <font>
        <pointsize>10</pointsize>
        <weight>75</weight>
        <bold>true</bold>
       </font>
      </property>
      <property name="styleSheet">
       <string notr="true">QPushButton#BotonClose{
border:0px;
background:none;
color:white;
}</string>
      </property>
      <property name="text">
       <string>X</string>
      </property>
     </widget>
     <widget class="QLabel" name="label_2">
      <property name="geometry">
       <rect>
        <x>2</x>
        <y>1</y>
        <width>151</width>
        <height>16</height>
       </rect>
      </property>
      <property name="styleSheet">
       <string notr="true">color:white;</string>
      </property>
      <property name="text">
       <string>Exxe-Ventas v3.0 2019.01.01</string>
      </property>
     </widget>
    </widget>
    <widget class="QLabel" name="LabelStatus">
     <property name="geometry">
      <rect>
       <x>0</x>
       <y>424</y>
       <width>481</width>
       <height>16</height>
      </rect>
     </property>
     <property name="styleSheet">
      <string notr="true">color:white;
background:qlineargradient(spread:pad, x1:1, y1:0.545, x2:0, y2:0.585, stop:0 rgba(184, 21, 21, 57), stop:0.487 rgba(182, 27, 13, 186), stop:1 rgba(184, 21, 21, 57));</string>
     </property>
     <property name="text">
      <string> Satus: Sin Conexion</string>
     </property>
     <property name="alignment">
      <set>Qt::AlignCenter</set>
     </property>
    </widget>
   </widget>
   <widget class="QFrame" name="frame2">
    <property name="geometry">
     <rect>
      <x>480</x>
      <y>0</y>
      <width>0</width>
      <height>443</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">QFrame#frame2{
border:0px;
border:2px solid grey;
background:white;
}</string>
    </property>
    <property name="frameShape">
     <enum>QFrame::StyledPanel</enum>
    </property>
    <property name="frameShadow">
     <enum>QFrame::Raised</enum>
    </property>
    <widget class="QLabel" name="label">
     <property name="geometry">
      <rect>
       <x>-3</x>
       <y>0</y>
       <width>141</width>
       <height>21</height>
      </rect>
     </property>
     <property name="styleSheet">
      <string notr="true">background:#0584B1;
color:white;</string>
     </property>
     <property name="text">
      <string>Configuración</string>
     </property>
     <property name="alignment">
      <set>Qt::AlignCenter</set>
     </property>
    </widget>
    <widget class="QLabel" name="label_3">
     <property name="geometry">
      <rect>
       <x>30</x>
       <y>30</y>
       <width>61</width>
       <height>16</height>
      </rect>
     </property>
     <property name="styleSheet">
      <string notr="true">color:grey;</string>
     </property>
     <property name="text">
      <string>Sucursal:</string>
     </property>
     <property name="alignment">
      <set>Qt::AlignCenter</set>
     </property>
    </widget>
    <widget class="QComboBox" name="ComboSuc">
     <property name="geometry">
      <rect>
       <x>30</x>
       <y>60</y>
       <width>69</width>
       <height>22</height>
      </rect>
     </property>
     <property name="styleSheet">
      <string notr="true">QComboBox#ComboSuc{
background:none;
border:0px;
border-bottom: 1px solid lightblue;
color:#05839C;
}


QComboBox QAbstractItemView
{
    border:0px;
    background:white;
    color: grey;

}
QComboBox#ComboSuc::drop-down
{
     subcontrol-origin: padding;
     subcontrol-position: top right;
     width: 15px;
     color: white;
     border-left-width: 0px;
     border-left-color: darkgray;
     border-left-style: solid; /* just a single line */
     border-top-right-radius: 3px; /* same radius as the QComboBox */
     border-bottom-right-radius: 3px;
     padding-left: 10px;
 }</string>
     </property>
     <property name="editable">
      <bool>true</bool>
     </property>
     <item>
      <property name="text">
       <string>Exxe 1</string>
      </property>
     </item>
     <item>
      <property name="text">
       <string>Exxe 3</string>
      </property>
     </item>
    </widget>
   </widget>
   <zorder>frame2</zorder>
   <zorder>frame1</zorder>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

1 个答案:

答案 0 :(得分:1)

几何形状是小部件相对于父级的位置,在初始情况下,几何形状为(480,0,0,443),即宽度为0,但位于frame1的右侧并将其设置为几何(0,0,0,0)后,将其移至窗口的左上位置,并应用了动画,但看不到为什么它位于frame1后面。

为更好地观察错误,请使用raise_,以使框架位于所有这些框架的顶部。

def animaAjustes(self):
    if self.frame2.width() != 0:
        self.frame2.setGeometry(0, 0, 0, 0)
    else:
        self.frame2.raise_() # <--- this change will make the error visible
        self.animation.start()

解决方案只是更改大小,而不是更改几何(几何是位置+大小):

def animaAjustes(self):
    if self.frame2.width() > 0:
        self.frame2.resize(0, 0)
    else:
        self.animation.start()