import time
from PySide6.QtWidgets import *
from PySide6.QtCore import *
from PySide6.QtGui import *
from page import Ui_Form
class Worker(QObject):
data_signal = Signal(int)
finish_signal = Signal()
def __init__(self):
super().__init__()
self.running = False
self.pausing = False
def workRun(self):
self.running = True
a = 0
while self.running:
if self.pausing:
time.sleep(0.01)
continue
time.sleep(0.1)
self.data_signal.emit(a)
a += 1
if a > 100: break
self.finish_signal.emit()
def stop(self):
self.running = False
def pause(self):
self.pausing = True
def resume(self):
self.pausing = False
class Window(QWidget, Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
#按鈕使能
self.pushButton_start.setEnabled(True)
self.pushButton_stop.setEnabled(False)
self.pushButton_pause.setEnabled(False)
self.pushButton_resume.setEnabled(False)
def onBtnStartClick(self):
self.work = Worker()
self.t =QThread()
self.work.moveToThread(self.t)
self.t.start()
#slot
self.work.data_signal.connect(lambda x:self.progressBar.setValue(x))
self.work.finish_signal.connect(self.on_work_finish)
self.t.started.connect(self.work.workRun)
self.t.finished.connect(self.on_thread_finish)
# 按鈕使能
self.pushButton_start.setEnabled(False)
self.pushButton_stop.setEnabled(True)
self.pushButton_pause.setEnabled(True)
self.pushButton_resume.setEnabled(True)
def on_work_finish(self):
self.t.quit()
self.t.wait()
print("完成")
def onBtnStopClick(self):
self.work.stop()
#按鈕使能
self.pushButton_start.setEnabled(True)
self.pushButton_stop.setEnabled(False)
self.pushButton_pause.setEnabled(False)
self.pushButton_resume.setEnabled(False)
def onBtnPauseClick(self):
self.work.pause()
def onBtnResumeClick(self):
self.work.resume()
def on_thread_finish(self):
self.work.deleteLater()
self.t.deleteLater()
print("線程銷毀")
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec())
承擔因您的行為而導致的法律責任,
本站有權保留或刪除有爭議評論。
參與本評論即表明您已經閱讀并接受
上述條款。