gmarzloff / animation-widget

PyQt6 widget - Animate through a set of frames while controlling frame rate in real time

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Animation Control with PyQt6

Introduction

This PyQt6 widget is a subclass of QLabel and provides a way to control animation through a set of images loaded from a directory.

The provided images are of a simple vector graphic rotated through 360 degrees, and while it would be more efficient to animate the rotation of one image, this widget is for more complex cycle of frames. For example, to simulate animation through frames of a pre-rendered 3D scene.

Screenshot

Installation

  1. Clone the repository
$ git clone git@github.com:gmarzloff/animation-widget.git
$ cd animation-widget
  1. create a virtual environment and load it:
$ python3 -m venv venv
$ ./venv/Scripts/activate
  1. install the packages in the requirements.txt file.
$ pip install -r requirements.txt
  1. run the main.py file either in PyCharm IDE or directly from the command line:
$ python main.py

Usage

Instantiate the widget where you build the GUI, passing the Path of the image directory. The widget sorts the files in the directory alphabetically and places them in that order for the animation.

parent_path = Path(__file__).parent
images_path = parent_path / 'images/animation_set/'
self.animation_widget = AnimationWidget(images_path)

call AnimationWidget.advance() on a Timer to move through the animation frames.

    def __init__(self):
        super().__init__()
        # ...
        self.refresh_interval = int(1000/frames_per_second)
        self.timer = QTimer()
        self.timer.timeout.connect(self.animation_handler)
        self.timer.start(self.refresh_interval)
    def animation_handler(self):
        self.animation_widget.advance()
        self.timer.start(self.refresh_interval)

Demo

$ python main.py

Drag the slider to change the animation's frame rate in real time.

About

PyQt6 widget - Animate through a set of frames while controlling frame rate in real time


Languages

Language:Python 100.0%