728x90

QTimer.singleShot 함수를 특정 시간 간격으로 여러번 실행하는 방법

들어가며

  • @PyQt5@에서 @QTimer.singleShot@ 함수를 특정 시간 간격으로 여러번 실행할 수 있다.
  • @QTimer.singleShot@ 함수는 밀리초 단위의 시간 지연을 준 후, 특정 함수를 실행해주는 함수이다. 

 

예제

  • 다음은 @QTimer.singleShot@ 함수를 3초 간격으로 여러 번 실행하는 예제이다.
  • 반복문재귀 호출을 이용하여 구현하였다.
from PyQt5.QtCore import QTimer

class MyClass:
    def __init__(self):
        self.count = 0

    def start_timer(self):
        self.count = 0
        self.run_timer()

    def run_timer(self):
        if self.count < 5:  # 원하는 실행 횟수 설정
            QTimer.singleShot(3000, self.run_timer)
            self.count += 1
            print("Timer executed:", self.count)
        else:
            print("Timer execution finished")

# MyClass 인스턴스 생성
obj = MyClass()
# 타이머 실행
obj.start_timer()
Timer executed: 1
Timer executed: 2
Timer executed: 3
Timer executed: 4
Timer executed: 5
Timer execution finished

 

참고 사이트

 

QTimer Class | Qt Core 6.5.2

 

doc.qt.io

 

728x90