Skip to content

animations

Class info

Classes

Name Children Inherits
Animator
prettyqt.animations.animator
Helper class to animate TabWidget / StackedWidget transistions and other.
    CursorMoveAnimation
    prettyqt.animations.cursormoveanimation
    Animation to move mouse cursor. None for start / end uses current position.
      Fx
      prettyqt.animations.fx
      Fx delegator which allows a quick and easy way to animate widgets.
        SlideAnimation
        prettyqt.animations.slideanimation
          TextAnimation
          prettyqt.animations.textanimation
            ZoomAnimation
            prettyqt.animations.zoomanimation

              🛈 DocStrings

              Package containing stuff related to animating widgets.

              # a lot of animations depend on current geometry.
              # It's advisable to show widget first so that it gets layouted.
              widget = widget.Label("I will get animated.")
              widget.show()
              
              # start a general fade animation with fixed start/end
              widget.fx["windowOpacity"].animate(start=0, end=1, duration=1000)
              
              # Transition widget size from current value to (500, 500)
              widget.fx["size"].transition_to((500, 500), duration=1000)
              
              # Transition widget size from current value to (500, 500)
              widget.fx["pos"].transition_from((0, -100), duration=1000)
              
              # fade-in when widget gets clicked
              widget.fx["windowOpacity"].animate_on_event("mouse_button_press", start=0, end=1)
              
              # convenience method for mentioned fade-in
              widget.fx.fade_in(start=0, end=1, start=500)
              
              # start a zoom animation
              widget.fx.zoom(start=1, end=1.5, duration=400)
              
              # start a slide animation 100px to the right
              widget.fx.slide(start=(0, 0), end=(100, 0), duration=400)
              

              PrettyQt makes it easy to animate your widgets. All animation-related methods are available via the Widget.fx attribute.

              Animating stylesheet values is also possible easily.

              TODO.

              Animator

              Bases: Object

              Helper class to animate TabWidget / StackedWidget transistions and other.

              Required interface:

              • currentIndex / setCurrentIndex
              • widget
              • indexOf
              • count
              • rect
              Source code in prettyqt/animations/animator.py
              class Animator(core.Object):
                  """Helper class to animate TabWidget / StackedWidget transistions and other.
              
                  Required interface:
              
                     * `currentIndex` / `setCurrentIndex`
                     * `widget`
                     * `indexOf`
                     * `count`
                     * `rect`
                  """
              
                  def __init__(self, widget):
                      super().__init__(widget)
                      self._widget = widget
                      self.direction = "horizontal"
                      self.speed = 500
                      self.animation_type = "out_cubic"
                      self.now = 0
                      self.next = 0
                      self.wrap = False
                      self.pnow = core.Point(0, 0)
                      self.active = False
              
                  def set_direction(self, direction: constants.OrientationStr):
                      self.direction = direction
              
                  def set_speed(self, speed: int):
                      self.speed = speed
              
                  def set_animation(self, animation_type: core.easingcurve.TypeStr):
                      self.animation_type = animation_type
              
                  def set_wrap(self, wrap: bool):
                      self.wrap = wrap
              
                  @core.Slot()
                  def slide_in_previous(self):
                      now = self._widget.currentIndex()
                      if self.wrap or now > 0:
                          self.slide_in(now - 1)
              
                  @core.Slot()
                  def slide_in_next(self):
                      now = self._widget.currentIndex()
                      if self.wrap or now < (self._widget.count() - 1):
                          self.slide_in(now + 1)
              
                  def slide_in(self, idx: int | widgets.QWidget):
                      if self.active:
                          return
                      if isinstance(idx, int):
                          if idx > (self._widget.count() - 1):
                              idx = idx % self._widget.count()
                          elif idx < 0:
                              idx = (idx + self._widget.count()) % self._widget.count()
                          idx = self._widget.widget(idx)
              
                      self.active = True
              
                      _now = self._widget.currentIndex()
                      _next = self._widget.indexOf(idx)
              
                      if _now == _next:
                          self.active = False
                          return
                      if hasattr(self._widget, "frameRect"):  # TabWidget doesnt have frameRect
                          frame_rect = self._widget.frameRect()
                      else:
                          frame_rect = self._widget.rect()
                      offsetx, offsety = (frame_rect.width(), frame_rect.height())
                      self._widget.widget(_next).setGeometry(frame_rect)
              
                      if self.direction == "horizontal":
                          if _now < _next:
                              offsetx, offsety = -offsetx, 0
                          else:
                              offsety = 0
              
                      elif _now < _next:
                          offsetx, offsety = 0, -offsety
                      else:
                          offsetx = 0
                      pnext = self._widget.widget(_next).pos()
                      pnow = self._widget.widget(_now).pos()
                      self.pnow = pnow
              
                      offset = core.Point(offsetx, offsety)
                      self._widget.widget(_next).move(pnext - offset)
                      self._widget.widget(_next).show()
                      self._widget.widget(_next).raise_()
              
                      anim_group = core.ParallelAnimationGroup(
                          self._widget, finished=self._on_animation_done
                      )
              
                      for index, start, end in zip(
                          (_now, _next), (pnow, pnext - offset), (pnow + offset, pnext)
                      ):
                          animation = core.PropertyAnimation(
                              self._widget.widget(index),
                              b"pos",
                              duration=self.speed,
                              easing_curve=self.animation_type,
                              start_value=start,
                              end_value=end,
                          )
                          anim_group.addAnimation(animation)
              
                      self.next = _next
                      self.now = _now
                      self.active = True
                      anim_group.start_animation(policy="delete")
              
                  @core.Slot()
                  def _on_animation_done(self):
                      self._widget.setCurrentIndex(self.next)
                      self._widget.widget(self.now).hide()
                      self._widget.widget(self.now).move(self.pnow)
                      self.active = False
              
                  def fade_in(self, widget: int | widgets.QWidget):
                      from prettyqt import custom_widgets
              
                      widget = self._widget.widget(widget) if isinstance(widget, int) else widget
                      self._widget.fader_widget = custom_widgets.FaderWidget(
                          self._widget.currentWidget(), widget, self.speed
                      )
                      self._widget.setCurrentWidget(widget)
              

              CursorMoveAnimation

              Bases: VariantAnimation

              Animation to move mouse cursor. None for start / end uses current position.

              Source code in prettyqt/animations/cursormoveanimation.py
              class CursorMoveAnimation(core.VariantAnimation):
                  """Animation to move mouse cursor. None for start / end uses current position."""
              
                  def __init__(
                      self,
                      duration: int = 1000,
                      start: datatypes.PointType | None = None,
                      end: datatypes.PointType = (0, 0),
                      easing: core.easingcurve.TypeStr = "in_out_sine",
                      **kwargs,
                  ):
                      super().__init__(**kwargs)
                      self.set_easing(easing)
                      self.set_start_value(start)
                      self.set_end_value(end)
                      self.setDuration(duration)
                      self.valueChanged.connect(gui.Cursor.setPos)
              
                  def set_start_value(self, point: datatypes.PointType | None):
                      """Set start position of mouse move."""
                      point = gui.Cursor.pos() if point is None else datatypes.to_point(point)
                      self.setStartValue(point)
              
                  def set_end_value(self, point: datatypes.PointType | None):
                      """Set start position of mouse move."""
                      point = gui.Cursor.pos() if point is None else datatypes.to_point(point)
                      self.setEndValue(point)
              

              set_end_value(point: datatypes.PointType | None)

              Set start position of mouse move.

              Source code in prettyqt/animations/cursormoveanimation.py
              def set_end_value(self, point: datatypes.PointType | None):
                  """Set start position of mouse move."""
                  point = gui.Cursor.pos() if point is None else datatypes.to_point(point)
                  self.setEndValue(point)
              

              set_start_value(point: datatypes.PointType | None)

              Set start position of mouse move.

              Source code in prettyqt/animations/cursormoveanimation.py
              def set_start_value(self, point: datatypes.PointType | None):
                  """Set start position of mouse move."""
                  point = gui.Cursor.pos() if point is None else datatypes.to_point(point)
                  self.setStartValue(point)
              

              Fx

              Fx delegator which allows a quick and easy way to animate widgets.

              Source code in prettyqt/animations/fx.py
              class Fx:
                  """Fx delegator which allows a quick and easy way to animate widgets."""
              
                  def __init__(self, widget: widgets.QWidget):
                      self._widget = widget
                      self._wrapper = None
                      # self._meta = core.MetaObject(self._widget.metaObject())
              
                  def __getitem__(self, value: str) -> AnimationWrapper:
                      value = helpers.to_lower_camel(value)
                      logger.debug("Building %r PropertyAnimation for %r", value, self._widget)
                      wrapper = AnimationWrapper(value, self)
                      self._wrapper = wrapper
                      return wrapper
              
                  # def __getattr__(self, attr):
                  #     return self.__getitem__(attr)
              
                  def set_colorize_effect(
                      self,
                      color: datatypes.ColorType,
                      strength: float = 0.7,
                  ) -> widgets.GraphicsColorizeEffect:
                      effect = widgets.GraphicsColorizeEffect(self._widget)
                      effect.setColor(colors.get_color(color))
                      effect.setStrength(strength)
                      self._widget.setGraphicsEffect(effect)
                      return effect
              
                  def set_opacity_effect(self, opacity: float) -> widgets.GraphicsOpacityEffect:
                      effect = widgets.GraphicsOpacityEffect(self._widget)
                      effect.setOpacity(opacity)
                      self._widget.setGraphicsEffect(effect)
                      return effect
              
                  def set_blur_effect(self, radius: int) -> widgets.GraphicsBlurEffect:
                      effect = widgets.GraphicsBlurEffect(self._widget)
                      effect.setBlurRadius(radius)
                      self._widget.setGraphicsEffect(effect)
                      return effect
              
                  def set_drop_shadow_effect(
                      self,
                      radius: int = 10,
                      color: datatypes.ColorType = "blue",
                  ) -> widgets.GraphicsDropShadowEffect:
                      effect = widgets.GraphicsDropShadowEffect(self._widget)
                      effect.setBlurRadius(radius)
                      effect.setColor(colors.get_color(color))
                      self._widget.setGraphicsEffect(effect)
                      return effect
              
                  def fade_in(
                      self,
                      duration: int = 1000,
                      easing: core.easingcurve.TypeStr = "in_out_sine",
                      delay: int = 0,
                  ) -> AnimationTimer:
                      """Trigger a fade-in animation."""
                      return self["windowOpacity"].transition_from(
                          0.0,
                          easing=easing,
                          duration=duration,
                          delay=delay,
                      )
              
                  def fade_out(
                      self,
                      duration: int = 1000,
                      easing: core.easingcurve.TypeStr = "in_out_sine",
                      delay: int = 0,
                  ) -> AnimationTimer:
                      """Trigger a fade-out animation."""
                      return self["windowOpacity"].transition_to(
                          0.0,
                          easing=easing,
                          duration=duration,
                          delay=delay,
                      )
              
                  def zoom(
                      self,
                      duration: int = 1000,
                      start: float = 1.0,
                      end: float = 1.0,
                      easing: core.easingcurve.TypeStr = "in_out_sine",
                      anchor: animations.zoomanimation.AnchorStr = "center",
                      delay: int = 0,
                  ) -> animations.ZoomAnimation:
                      """Trigger a zoom animation with given anchor."""
                      from prettyqt import animations
              
                      anim = animations.ZoomAnimation(parent=self._widget, anchor=anchor)
                      anim.set_easing(easing)
                      anim.set_start_value(start)
                      anim.set_end_value(end)
                      anim.set_duration(duration)
                      self.run(anim, delay)
                      return anim
              
                  def slide(
                      self,
                      duration: int = 1000,
                      start=None,
                      end=None,
                      easing: core.easingcurve.TypeStr = "in_out_sine",
                      delay: int = 0,
                      reverse: bool = False,
                      single_shot: bool = True,
                  ) -> AnimationTimer:
                      anim = core.PropertyAnimation(parent=self._widget)
                      anim.set_easing(easing)
                      pos = self._widget.geometry().topLeft()
                      start_offset = core.Point(0, 0) if start is None else datatypes.to_point(start)
                      end_offset = core.Point(0, 0) if end is None else datatypes.to_point(end)
                      anim.set_start_value(pos + start_offset)
                      anim.set_end_value(pos + end_offset)
                      anim.setDuration(duration)
                      anim.apply_to(self._widget.pos)
                      if reverse:
                          anim.append_reversed()
                      return self.run(anim, delay, single_shot=single_shot)
              
                  def bounce(
                      self,
                      end: datatypes.PointType,
                      easing: core.easingcurve.TypeStr = "in_out_sine",
                      duration: int = 1000,
                      delay: int = 0,
                  ) -> AnimationTimer:
                      """Trigger a move animation to given offset and return to original position."""
                      return self["pos"].transition_to(
                          end,
                          easing=easing,
                          duration=duration,
                          delay=delay,
                          reverse=True,
                      )
              
                  def run(
                      self,
                      animation: core.QPropertyAnimation | core.AnimationGroupMixin,
                      delay: int | str = 0,
                      single_shot: bool = True,
                  ) -> AnimationTimer:
                      """Run an animation with given delay.
              
                      Arguments:
                          animation: Animation to run
                          delay: delay after which animation should start
                          single_shot: whethere the animation should trigger once or repeat
              
                      Returns:
                          AnimationTimer, a core.Timer subclass with the PropertyAnimation attached.
                      """
                      target = animation.targetObject()
                      if isinstance(target, widgets.QWidget) and not target.isVisible():
                          logger.info("Attention. Starting animation for invisible widget.")
                      msg = "starting %s with delay %s, single_shot %s"
                      logger.debug(msg, animation, delay, single_shot)
                      timer = AnimationTimer(
                          parent=self._widget,
                          single_shot=single_shot,
                          interval=helpers.parse_time(delay) if isinstance(delay, str) else delay,
                          animation=animation,
                      )
                      timer.start()
                      return timer
              
                  def highlight_widget(self, widget: widgets.QWidget):
                      from prettyqt.custom_widgets.overlayborder import FocusWidget
              
                      overlay = FocusWidget(self._widget, widget)
                      overlay.show()
                      return overlay
              

              bounce(end: datatypes.PointType, easing: core.easingcurve.TypeStr = 'in_out_sine', duration: int = 1000, delay: int = 0) -> AnimationTimer

              Trigger a move animation to given offset and return to original position.

              Source code in prettyqt/animations/fx.py
              def bounce(
                  self,
                  end: datatypes.PointType,
                  easing: core.easingcurve.TypeStr = "in_out_sine",
                  duration: int = 1000,
                  delay: int = 0,
              ) -> AnimationTimer:
                  """Trigger a move animation to given offset and return to original position."""
                  return self["pos"].transition_to(
                      end,
                      easing=easing,
                      duration=duration,
                      delay=delay,
                      reverse=True,
                  )
              

              fade_in(duration: int = 1000, easing: core.easingcurve.TypeStr = 'in_out_sine', delay: int = 0) -> AnimationTimer

              Trigger a fade-in animation.

              Source code in prettyqt/animations/fx.py
              def fade_in(
                  self,
                  duration: int = 1000,
                  easing: core.easingcurve.TypeStr = "in_out_sine",
                  delay: int = 0,
              ) -> AnimationTimer:
                  """Trigger a fade-in animation."""
                  return self["windowOpacity"].transition_from(
                      0.0,
                      easing=easing,
                      duration=duration,
                      delay=delay,
                  )
              

              fade_out(duration: int = 1000, easing: core.easingcurve.TypeStr = 'in_out_sine', delay: int = 0) -> AnimationTimer

              Trigger a fade-out animation.

              Source code in prettyqt/animations/fx.py
              def fade_out(
                  self,
                  duration: int = 1000,
                  easing: core.easingcurve.TypeStr = "in_out_sine",
                  delay: int = 0,
              ) -> AnimationTimer:
                  """Trigger a fade-out animation."""
                  return self["windowOpacity"].transition_to(
                      0.0,
                      easing=easing,
                      duration=duration,
                      delay=delay,
                  )
              

              run(animation: core.QPropertyAnimation | core.AnimationGroupMixin, delay: int | str = 0, single_shot: bool = True) -> AnimationTimer

              Run an animation with given delay.

              Parameters:

              Name Type Description Default
              animation QPropertyAnimation | AnimationGroupMixin

              Animation to run

              required
              delay int | str

              delay after which animation should start

              0
              single_shot bool

              whethere the animation should trigger once or repeat

              True
              Source code in prettyqt/animations/fx.py
              def run(
                  self,
                  animation: core.QPropertyAnimation | core.AnimationGroupMixin,
                  delay: int | str = 0,
                  single_shot: bool = True,
              ) -> AnimationTimer:
                  """Run an animation with given delay.
              
                  Arguments:
                      animation: Animation to run
                      delay: delay after which animation should start
                      single_shot: whethere the animation should trigger once or repeat
              
                  Returns:
                      AnimationTimer, a core.Timer subclass with the PropertyAnimation attached.
                  """
                  target = animation.targetObject()
                  if isinstance(target, widgets.QWidget) and not target.isVisible():
                      logger.info("Attention. Starting animation for invisible widget.")
                  msg = "starting %s with delay %s, single_shot %s"
                  logger.debug(msg, animation, delay, single_shot)
                  timer = AnimationTimer(
                      parent=self._widget,
                      single_shot=single_shot,
                      interval=helpers.parse_time(delay) if isinstance(delay, str) else delay,
                      animation=animation,
                  )
                  timer.start()
                  return timer
              

              zoom(duration: int = 1000, start: float = 1.0, end: float = 1.0, easing: core.easingcurve.TypeStr = 'in_out_sine', anchor: animations.zoomanimation.AnchorStr = 'center', delay: int = 0) -> animations.ZoomAnimation

              Trigger a zoom animation with given anchor.

              Source code in prettyqt/animations/fx.py
              def zoom(
                  self,
                  duration: int = 1000,
                  start: float = 1.0,
                  end: float = 1.0,
                  easing: core.easingcurve.TypeStr = "in_out_sine",
                  anchor: animations.zoomanimation.AnchorStr = "center",
                  delay: int = 0,
              ) -> animations.ZoomAnimation:
                  """Trigger a zoom animation with given anchor."""
                  from prettyqt import animations
              
                  anim = animations.ZoomAnimation(parent=self._widget, anchor=anchor)
                  anim.set_easing(easing)
                  anim.set_start_value(start)
                  anim.set_end_value(end)
                  anim.set_duration(duration)
                  self.run(anim, delay)
                  return anim