import time
from PySide6.QtWidgets import *
from PySide6.QtCore import *
from PySide6.QtGui import *
from page import Ui_Form
class MyThread(QThread):
data_singal = Signal(int)
finish_singal = Signal()
def __init__(self):
super().__init__()
self.running = False
self.pausing= False
def run(self):
a = 0
self.running = True
while self.running:
if self.pausing:
time.sleep(0.01)
continue
self.data_singal.emit(a)
time.sleep(0.1)
a+=1
if a>100:break
self.finish_singal.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.t = MyThread()
self.t.start()
self.t.data_singal.connect(lambda x:self.progressBar.setValue(x))
self.t.finish_singal.connect(self.on_work_thread)
self.t.finished.connect(lambda :self.t.deleteLater())
# 按鈕使能
self.pushButton_start.setEnabled(False)
self.pushButton_stop.setEnabled(True)
self.pushButton_pause.setEnabled(True)
self.pushButton_resume.setEnabled(True)
def onBtnStopClick(self):
self.t.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.t.pause()
def onBtnResumeClick(self):
self.t.resume()
def on_work_thread(self):
self.t.quit()
self.t.wait()
print("線程結(jié)束")
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec())
承擔(dān)因您的行為而導(dǎo)致的法律責(zé)任,
本站有權(quán)保留或刪除有爭(zhēng)議評(píng)論。
參與本評(píng)論即表明您已經(jīng)閱讀并接受
上述條款。