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

파이썬

python tkinter gui 만들기

2023년 05월 10일

Tkinter는 파이썬에서 제공하는 GUI 라이브러리로, Tcl/Tk 라이브러리를 파이썬에서 사용할 수 있게 합니다. 기본 라이브러리에 포함되어 있어 별도 설치가 필요하지 않습니다. 자, 그럼 python tkinter gui 만들기 실습해 보겠습니다.

Read More
파이썬

파이썬에서의 데이터 시각화와 그래프 표현 방법

2024년 05월 07일

파이썬에서 데이터 시각화와 그래프 표현은 데이터 분석과 시각적 표현에 있어 중요한 역할을 합니다. 파이썬은 다양한 라이브러리를 제공하여 데이터를 시각적으로 나타내거나 그래프로 표현할 수 있게 해줍니다. 데이터 시각화를 통해 숨겨진 트렌드나 패턴을 발견할 수 있고, 결정을 내릴 때 도움이 될 수 있습니다. 그러나 시각화는 데이터를 이해하는 데에만 중요한 것이 아니라 결과를 효과적으로 전달하기 위해서도 중요합니다. 이를 통해 데이터 분석 결과를 이해하기 쉽게 전달할 수 있으며, 의사 결정에 활용될 수 있습니다. 이에 따라 파이썬에서 데이터 시각화와 그래프 표현 방법을 학습하는 것은 데이터 분석에 있어 유용한 기술을 습득하는 데 큰 도움이 될 것입니다.

Read More

PyQt6를 사용한 GUI 애플리케이션 개발과 DevOps

2023년 07월 07일

효율적으로 관리하고 배포하기 위해서는 DevOps 원칙을 도입하는 것이 좋습니다. 이 글에서는 PyQt6를 사용하여 GUI 애플리케이션을 구축하는 동시에, DevOps를 통해 지속적인 통합 및 배포를 구현하는 방법에 대해 알아보겠습니다.

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