import threading
import time
from PySide6.QtWidgets import *
from PySide6.QtCore import *
from PySide6.QtGui import *
from page import Ui_Form
from threading import Thread
class WorkThread(Thread):
def __init__(self, win):
super().__init__()
self.running = True
self.pausing = False
self.win: Window = win
def run(self):
a = 0
while self.running:
if self.pausing:
time.sleep(0.01)
continue
self.win.data_singal.emit(a)
time.sleep(0.2)
a += 1
if a > 100: break
self.running = True
def stop(self):
self.running = False
def pause(self):
self.pausing = True
def resume(self):
self.pausing = False
class Window(QWidget, Ui_Form):
data_singal = Signal(int)
def __init__(self):
super().__init__()
self.setupUi(self)
self.data_singal.connect(self.change_value)
self.t: threading.Thread = None
def onBtnStartClick(self):
if self.t is not None and self.t.is_alive(): return
self.t = WorkThread(self)
# self.t.daemon= True
self.t.start()
def onBtnStopClick(self):
if self.t is not None and self.t.is_alive():
self.t.stop()
def onBtnPauseClick(self):
if self.t is not None and self.t.is_alive():
self.t.pause()
def onBtnResumeClick(self):
if self.t is not None and self.t.is_alive():
self.t.resume()
def change_value(self, num: int):
self.progressBar.setValue(num)
def closeEvent(self, event):
if self.t is not None and self.t.is_alive():
self.t.stop()
self.t.join()
self.t = None
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec())
承擔(dān)因您的行為而導(dǎo)致的法律責(zé)任,
本站有權(quán)保留或刪除有爭議評(píng)論。
參與本評(píng)論即表明您已經(jīng)閱讀并接受
上述條款。