Skip to content
toylee blog · 컴퓨터, 프로그램 정보 공유

toylee blog · 컴퓨터, 프로그램 정보 공유

PyQt6 QPropertyAnimation Framework

toylee, 2023년 05월 25일

GUI의 기능성은 중요하지만, 눈에 띄는 시각적인 요소도 중요합니다. GUI 요소에 애니메이션과 동적인 움직임을 추가하여 시각적으로 매력적인 GUI를 만들 수 있습니다. 이 글에서는 PyQt6 QPropertyAnimation Framework 를 살펴보고 GUI 안에 움직이는 방법에 대해 같이 알아보겠습니다.

[목차]

  • 1. PyQt6 QPropertyAnimation Framework 란?
    • 1) QPropertyAnimation
    • 2) QGraphicsOpacityAnimation
    • 3) QSequentialAnimationGroup
  • 2. PyQt6 Animation 응용 예시

1. PyQt6 QPropertyAnimation Framework 란?

PyQt6 애니메이션 프레임워크는 Qt의 애니메이션 프레임워크를 기반으로 구축되었으며, GUI에 애니메이션을 만드는 간단하고 직관적인 방법을 제공합니다. PyQt6 애니메이션 프레임워크의 기본 구성 요소는 QPropertyAnimation, QGraphicsOpacityAnimation, QSequentialAnimationGroup 세 가지입니다.

1) QPropertyAnimation

QPropertyAnimation은 QObject의 어떤 속성(위치, 크기 또는 색상)을 애니메이션화하는 데 사용됩니다. 지정된 기간 동안 현재 값과 대상 값 사이를 보간하여 작동합니다.

2) QGraphicsOpacityAnimation

QGraphicsOpacityAnimation은 QGraphicsItem 객체의 불투명도를 애니메이션화하는 데 사용됩니다. 값 사이를 보간하는 대신 불투명도 레벨 사이를 보간합니다.

3) QSequentialAnimationGroup

QSequentialAnimationGroup는 여러 애니메이션을 그룹화하여 순차적으로 재생합니다. 동시에 여러 애니메이션을 재생하기 위해 QParallelAnimationGroup도 사용할 수 있습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import sys
from PyQt6.QtWidgets import QApplication, QWidget
from PyQt6.QtCore import QPropertyAnimation, QRect, QSize, Qt
from PyQt6.QtGui import QColor, QPainterPath, QPainter, QPaintEvent
from random import randint
 
 
class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.resize(600, 600)
        self.child = QWidget(self)
        self.child.setGeometry(QRect(250, 250, 100, 100))
 
        self.path = QPainterPath()
        self.path.moveTo(300, 300)
        self.path.cubicTo(400, 100, 200, 400, 300, 500)
 
        self.anim = QPropertyAnimation(self.child, b“pos”)
        self.anim.setStartValue(self.path.pointAtPercent(0))
        self.anim.setEndValue(self.path.pointAtPercent(1))
        self.anim.setDuration(2000)
 
        self.anim.finished.connect(self.animateFinished)
 
        self.anim.start()
        self.changeBackgroundColor()
 
    def animateFinished(self):
        self.path = QPainterPath()
        self.path.moveTo(self.path.pointAtPercent(1))
        points = [
            (100, 100),
            (300, 100),
            (400, 300),
            (500, 100),
            (700, 100),
            (500, 300),
            (700, 500),
            (500, 500),
            (400, 700),
            (300, 500),
            (100, 500),
            (300, 300)
        ]
        for i in range(len(points)):
            if i < len(points) – 1:
                self.path.lineTo(points[i][0], points[i][1])
 
        self.anim.setStartValue(self.path.pointAtPercent(0))
        self.anim.setEndValue(self.path.pointAtPercent(1))
        self.anim.start()
        self.changeBackgroundColor()
 
    def changeBackgroundColor(self):
        r = randint(0, 255)
        g = randint(0, 255)
        b = randint(0, 255)
        color = QColor(r, g, b)
        self.child.setStyleSheet(f“background-color: {color.name()};border-radius:15px;”)
 
 
if __name__ == “__main__”:
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec())
 
Colored by Color Scripter
cs
PyQt6 QPropertyAnimation1
PyQt6 QPropertyAnimation2
PyQt6 QPropertyAnimation3

2. PyQt6 Animation 응용 예시

PyQt6를 사용하여 간단한 통계를 보여주는 애플리케이션을 구현한 예제입니다. 코드를 살펴보면 다음과 같은 구성 요소가 있습니다

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import sys
import random
import time
from PyQt6.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QProgressBar, QLabel
from PyQt6.QtCore import Qt, QPropertyAnimation, QThread, pyqtSignal, pyqtSlot
 
 
class StatisticsWidget(QWidget):
    def __init__(self):
        super().__init__()
 
        self.values = [0] * 10
        self.bars = []
        self.labels = []
 
        vbox_layout = QVBoxLayout()
 
        for i in range(10):
            bar = QProgressBar()
            bar.setRange(0, 100)
            bar.setTextVisible(False)
            bar.setAlignment(Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
            vbox_layout.addWidget(bar)
            self.bars.append(bar)
 
            label = QLabel()
            vbox_layout.addWidget(label)
            self.labels.append(label)
 
        self.setLayout(vbox_layout)
 
    def update_statistics(self, data):
        self.values[data // 10] += 1
 
        total_values = sum(self.values)
 
        for i in range(10):
            bar = self.bars[i]
            label = self.labels[i]
            value = self.values[i]
            percentage = (value / total_values) * 100 if total_values != 0 else 0
            bar.setRange(0, total_values)
            bar.setValue(value)
            label.setText(f“{value}/{total_values} ({percentage:.2f}%)”)
 
 
class DataGenerator(QThread):
    updated = pyqtSignal(int)
 
    def run(self):
        while True:
            data = random.randint(0, 100)
            self.updated.emit(data)
            time.sleep(0.1)
 
 
class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setGeometry(800, 200, 320, 340)
 
        self.statistics_widget = StatisticsWidget()
 
        vbox_layout = QVBoxLayout()
        vbox_layout.addWidget(self.statistics_widget)
 
        central_widget = QWidget()
        central_widget.setLayout(vbox_layout)
        self.setCentralWidget(central_widget)
 
        self.data_generator = DataGenerator()
        self.data_generator.updated.connect(self.update_statistics)
        self.data_generator.start()
 
    @pyqtSlot(int)
    def update_statistics(self, data):
        self.statistics_widget.update_statistics(data)
 
 
if __name__ == “__main__”:
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())
 
Colored by Color Scripter
cs

[실행화면]

PyQt6 QPropertyAnimation

StatisticsWidget: 통계를 보여주는 위젯입니다. 10개의 프로그래스 바와 레이블로 구성되어 있습니다. update_statistics 메서드를 통해 통계 데이터를 받아 각각의 프로그래스 바와 레이블을 업데이트합니다.

DataGenerator: 무작위 데이터를 생성하는 백그라운드 스레드입니다. run 메서드에서 무작위로 0부터 100 사이의 데이터를 생성하고, updated 시그널을 발생시켜 데이터를 전달합니다.

MainWindow: 메인 창으로서 StatisticsWidget을 포함하고 있습니다. DataGenerator를 생성하고 updated 시그널과 update_statistics 슬롯을 연결하여 통계 데이터를 업데이트합니다.

애플리케이션이 실행되면 MainWindow가 생성되고, DataGenerator가 백그라운드에서 데이터를 생성하여 StatisticsWidget을 업데이트합니다. 각 프로그래스 바는 해당 데이터 범위 내에서의 횟수를 나타내며, 레이블은 해당 횟수와 전체 횟수에 대한 백분율을 보여줍니다.

[관련글]

파이썬 PyQt6 레이아웃 배치

PyQt6 QT Designer 사용자 지정 위젯

파이썬 Tkinter 파일 불러오기-filedialog

PyQt6 계산기 만들기

파이썬

글 내비게이션

Previous post
Next post

Related Posts

파이썬

오디오 재생 – QMediaPlayerpyqt

2023년 06월 08일

PyQt를 사용하면 멀티미디어를 지원하므로 GUI에서 오디오 재생하는 것도 가능합니다. 이번 게시물에서는 PyQt QMediaPlayerpyqt 사용하여 오디오 재생 하는 방법에 대해 알아보겠습니다.

Read More
파이썬

파이썬 프로그래밍에서 활용되는 가장 유용한 라이브러리들

2024년 05월 07일

파이썬은 다양한 분야에서 널리 사용되는 프로그래밍 언어입니다. 파이썬의 강력한 특징 중 하나는 다양한 라이브러리들을 활용할 수 있다는 점입니다. 이 글에서는 파이썬 프로그래밍에서 활용되는 가장 유용한 라이브러리들에 대해 살펴보겠습니다. 다양한 작업을 자동화하고 개발 시간을 단축하는데 도움이 되는 라이브러리들이 많이 있습니다. 이러한 라이브러리들을 잘 활용하면 더욱 효율적인 프로그래밍을 할 수 있습니다. 파이썬의 인기와 함께 라이브러리들도 계속 발전하고 있어, 프로그래머들은 더 다양한 옵션을 선택할 수 있습니다. 이를 통해 빠르고 효과적인 소프트웨어 개발이 가능해지고 있습니다.

Read More
파이썬

파이썬 기초 문법과 활용 예제

2024년 05월 07일

파이썬은 강력하고 다양한 기능을 가진 프로그래밍 언어로, 기초 문법을 이해하고 활용하는 것은 프로그래밍을 시작하는 데 있어 중요한 첫걸음입니다. 이 글에서는 파이썬의 기초 문법과 활용 예제에 대해 자세히 살펴보겠습니다. 파이썬을 공부하면서 코딩의 세계에 입문하고 싶은 분들에게 도움이 될 것이라고 기대합니다. 파이썬은 간결하고 가독성이 높아서 많은 개발자들이 선택하는 언어 중 하나입니다. 변수, 조건문, 반복문 등의 기본적인 문법을 이해하면 간단한 프로그램을 작성할 수 있으며, 더 나아가 다양한 프로젝트를 구현할 수 있게 됩니다. 또한 파이썬은 데이터 분석, 인공지능, 웹 개발 등 다양한 분야에서 활용되므로, 학습하는 가치가 크다고 할 수 있습니다. 이 문서를 통해 파이썬의 기초 문법을 이해하고 활용하는 방법을 자세히 알아보겠습니다.

Read More

답글 남기기 응답 취소

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다

최신 글

  • 드론 비행금지구역에 대해 알아볼게요
  • cpu 온도 측정 방법
  • 포토샵 단축키 모음 정리본
  • express vpn이란? 장점 및 단점
  • 안드로이드 버전 업그레이드 방법

최신 댓글

  1. 윈도우 단축키 모음 Best5의 ace
  2. http https 차이의 챗GPT 란? · Working for you

보관함

  • 2025년 7월
  • 2025년 6월
  • 2025년 5월
  • 2025년 4월
  • 2025년 3월
  • 2025년 2월
  • 2025년 1월
  • 2024년 12월
  • 2024년 11월
  • 2024년 8월
  • 2024년 6월
  • 2024년 5월
  • 2024년 3월
  • 2024년 2월
  • 2023년 11월
  • 2023년 9월
  • 2023년 8월
  • 2023년 7월
  • 2023년 6월
  • 2023년 5월
  • 2023년 4월
  • 2023년 3월
  • 2023년 2월

카테고리

  • flutter
  • html
  • linux
  • macbook
  • Pc Useful Tips
  • 미분류
  • 워드프레스
  • 자바(Java)
  • 파이썬
  • 프로그래밍
©2025 toylee blog · 컴퓨터, 프로그램 정보 공유 | WordPress Theme by SuperbThemes