pyqt6 动画无法正常工作为什么?

问题描述 投票:0回答:1

当我按下按钮时,我做了一个切换菜单,它会展开(增加高度)和收缩(降低高度)。那是我所期望的。事实证明它可以扩展菜单但不能缩小菜单。

我将 QFrame 设置为最小值:W 201 H 0 最大值:W 201 H 0

def toggleMenu(self,maxHeight,enable): 
    if enable : 
      #get height 
      height = self.ui.LeftMenu.height() 
      print(height)
      maxExtend = maxHeight 
      standard = 0

      #set height

      if height == 0 : 
        heightExtended = maxExtend 
      else: 
        heightExtended = standard 

    
    

      # Animation  
      print(heightExtended)
      self.animation = QPropertyAnimation(self.ui.LeftMenu, b"minimumHeight") 
      self.animation.setDuration(200) 
      self.animation.setStartValue(height) 
      self.animation.setEndValue(heightExtended) 
      self.animation.setEasingCurve(QEasingCurve.Type.InOutQuart)
      self.animation.start() 

enter image description here

python pyqt pyqt6 qpropertyanimation
1个回答
0
投票

这是我得到的,它有效!!! 希望它能帮助其他人解决类似的问题;-;

  def toggleMenu(self, maxHeight, enable):
      if enable:
          # Get current height
          currentHeight = self.ui.LeftMenu.height()
          
          # Define the start and end values for the animation
          startValue = currentHeight
          endValue = maxHeight if currentHeight == 0 else 0
          
          # Create the animation
          animation = QVariantAnimation(self)
          animation.setStartValue(startValue)
          animation.setEndValue(endValue)
          animation.setDuration(200)
          animation.setEasingCurve(QEasingCurve.InOutQuart)
          
          # Define the function to be called on each animation frame
          def onValueChanged(value):
              self.ui.LeftMenu.setFixedHeight(value)

          
          # Connect the animation to the function
          animation.valueChanged.connect(onValueChanged)
          
          # Start the animation
          animation.start()
© www.soinside.com 2019 - 2024. All rights reserved.