PySide2 QTimer

QTimer

PySide2.QtCore.QTimer 클래슀λ₯Ό μ‚¬μš©ν•˜λ©΄ 반볡적인 μž‘μ—…μ΄λ‚˜ 일정 μ‹œκ°„ ν›„ μ‹€ν–‰λ˜λŠ” μž‘μ—…μ„ μ²˜λ¦¬ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

Single shot timer

singleShot()은 일정 μ‹œκ°„μ΄ μ§€λ‚œ ν›„, μž‘μ—…μ΄ μ‹œμž‘λ˜λ„λ‘ ν•  수 있게 ν•΄μ£ΌλŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€. static ν•¨μˆ˜μ΄κΈ° λ•Œλ¬Έμ— μΈμŠ€ν„΄μŠ€λ₯Ό λ§Œλ“€μ§€ μ•Šκ³  μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

μ•„λž˜ μ½”λ“œλŠ” ν‘Έμ‹œ λ²„νŠΌκ³Ό ν…μŠ€νŠΈ λΈŒλΌμš°μ € μœ„μ ―μ΄ μžˆμ„ λ•Œ, λ²„νŠΌμ„ λˆ„λ₯΄λ©΄ 1 초 후에 ν…μŠ€νŠΈλ₯Ό λ„μš°λŠ” μ½”λ“œμž…λ‹ˆλ‹€.

import sys
from PySide2.QtWidgets import QMainWindow, QApplication
from PySide2.QtCore import QTimer, SLOT
from ui_mainwindow import Ui_MainWindow
class Main_window(QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.push_button_clicked)
def push_button_clicked(self):
"""
1 초 νƒ€μž„μ•„μ›ƒκ³Ό push_button_clicked_single_shot μ—°κ²°
QTimer.singleShot(1000, self, SLOT("push_button_clicked_single_shot()"))
QTimer.singleShot(
1000, Qt.PreciseTimer, self, SLOT("push_button_clicked_single_shot()")
)
"""
QTimer.singleShot(1000, self.push_button_clicked_single_shot)
def push_button_clicked_single_shot(self):
self.textBrowser.append("a second later")
if __name__ == "__main__":
app = QApplication(sys.argv)
main_window = Main_window()
main_window.show()
sys.exit(app.exec_())
info

Linux와 Windowsμ—μ„œ νƒ€μ΄λ¨ΈλŠ” 차이가 μžˆμŠ΅λ‹ˆλ‹€.
Qt.TimerType: https://doc.qt.io/qtforpython/PySide2/QtCore/Qt.html

Repetitive timer

반볡 싀행은 μΈμŠ€ν„΄μŠ€ ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜κΈ° λ•Œλ¬Έμ— QTimer의 μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•΄μ•Όν•©λ‹ˆλ‹€.

μ•„λž˜ μ½”λ“œλŠ” ν‘Έμ‹œ λ²„νŠΌκ³Ό ν…μŠ€νŠΈ λΈŒλΌμš°μ € μœ„μ ―μ΄ μžˆμ„ λ•Œ, λ²„νŠΌμ„ λˆ„λ₯΄λ©΄ 1 초 κ°„κ²©μœΌλ‘œ 3 회 ν…μŠ€νŠΈλ₯Ό λ„μš°λŠ” μ½”λ“œμž…λ‹ˆλ‹€.

import sys
from PySide2.QtWidgets import QMainWindow, QApplication
from PySide2.QtCore import QTimer
from ui_mainwindow import Ui_MainWindow
class Main_window(QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.push_button_clicked)
self.is_push_button_clicked = False
self.push_button_timer = QTimer(self)
self.push_button_timer.setInterval(1000)
self.push_button_timer.timeout.connect(
self.push_button_clicked_repetitive
)
self.count = 0
def push_button_clicked(self):
if not self.is_push_button_clicked:
self.is_push_button_clicked = True
self.pushButton.setDown(True)
"""
반볡 μ‹œμž‘
"""
self.push_button_timer.start()
def push_button_clicked_repetitive(self):
self.count += 1
self.textBrowser.append(str(self.count) + " seconds later")
if self.count == 3:
"""
반볡 μ’…λ£Œ
"""
self.push_button_timer.stop()
self.is_push_button_clicked = False
self.pushButton.setDown(False)
self.count = 0
if __name__ == "__main__":
app = QApplication(sys.argv)
main_window = Main_window()
main_window.show()
sys.exit(app.exec_())

Reference

Last updated on