Created
July 12, 2023 13:29
-
-
Save mr-yoo/e61378583212f1efc36ea3546ac0d31c to your computer and use it in GitHub Desktop.
Thread 종료 기능 추가
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton | |
from PyQt5.QtCore import QThread | |
import time | |
class Worker(QThread): | |
def run(self): | |
while True: | |
if self.isInterruptionRequested(): # 인터럽트 확인 | |
print("terminate") | |
break | |
print('lol') | |
time.sleep(1) | |
class MyWin(QMainWindow): | |
def __init__(self): | |
super().__init__() | |
self.btn_s = QPushButton("start", self) | |
self.btn_s.clicked.connect(self.clickStartBtn) | |
self.btn_e = QPushButton("end", self) | |
self.btn_e.move(0, 40) | |
self.btn_e.clicked.connect(self.clickEndBtn) | |
def clickStartBtn(self): | |
self.w = Worker() | |
self.w.start() | |
def clickEndBtn(self): | |
self.w.requestInterruption() | |
app = QApplication([]) | |
w = MyWin() | |
w.show() | |
app.exec() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment