Skip to main content

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 MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pushButton.setText("λ²„νŠΌ 1")
self.pushButton.clicked.connect(self.pushButtonClicked)
def pushButtonClicked(self):
"""
1 초 νƒ€μž„μ•„μ›ƒκ³Ό appendTextBrowser μ—°κ²°
QTimer.singleShot(1000, self, SLOT("appendTextBrowser()"))
QTimer.singleShot(
1000, Qt.PreciseTimer, self, SLOT("appendTextBrowser()")
)
"""
QTimer.singleShot(1000, self.appendTextBrowser)
def appendTextBrowser(self):
self.textBrowser.append("1 초 ν›„")
if __name__ == "__main__":
app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.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 MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pushButton.setText("λ²„νŠΌ 1")
self.pushButton.clicked.connect(self.pushButtonClicked)
self.isPushButtonClicked = False
self.pushButtonTimer = QTimer(self)
self.pushButtonTimer.setInterval(1000)
self.pushButtonTimer.timeout.connect(self.appendTextBrowser)
self.count = 0
def pushButtonClicked(self):
if not self.isPushButtonClicked:
self.isPushButtonClicked = True
self.pushButton.setEnabled(False)
"""
반볡 μ‹œμž‘
"""
self.pushButtonTimer.start()
def appendTextBrowser(self):
self.count += 1
self.textBrowser.append(str(self.count) + " 초 ν›„")
if self.count == 3:
"""
반볡 μ’…λ£Œ
"""
self.pushButtonTimer.stop()
self.isPushButtonClicked = False
self.pushButton.setEnabled(True)
self.count = 0
if __name__ == "__main__":
app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())

Reference#

Last updated on