From 57be4757caec469fbd451051c20035d066a9f96d Mon Sep 17 00:00:00 2001 From: Alexander Date: Fri, 25 Aug 2023 00:52:29 +0800 Subject: [PATCH] add thread --- test.py | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/test.py b/test.py index 94ddf5d..d324e43 100644 --- a/test.py +++ b/test.py @@ -258,6 +258,18 @@ def run(self): self.signal.emit(translation) +class CaptureThread(QThread): + capture_done = pyqtSignal(str) + + def __init__(self, snipping_tool): + super().__init__() + self.snipping_tool = snipping_tool + + def run(self): + # Perform the capture here + result = self.snipping_tool.capture() + self.capture_done.emit(result) + class SnippingTool(QMainWindow): def __init__(self, main_window, ttsjp, ttsen): super().__init__() @@ -267,6 +279,9 @@ def __init__(self, main_window, ttsjp, ttsen): self.ttsjp, self.ttsen = ttsjp, ttsen + self.capture_thread = CaptureThread(self) + self.capture_thread.capture_done.connect(self.on_capture_done) + def initUI(self): self.setWindowTitle('Snipping Tool') self.setWindowOpacity(0.5) @@ -292,7 +307,17 @@ def mouseMoveEvent(self, event): def mouseReleaseEvent(self, event): if event.button() == Qt.LeftButton: - self.capture() + # self.capture() + self.capture_thread.start() + + def on_capture_done(self, text): + # Update the GUI after the capture is done + if self.main_window.translate_checkbox.isChecked(): + print("Pre-translated text: "+text) + # add a divider + self.main_window.append_console_text("=========================") + self.main_window.append_console_text("Pre-translated text: "+text) + self.translate_and_copy_to_clipboard(text) def capture(self): rect = self.rubber_band.geometry() @@ -352,12 +377,7 @@ def capture(self): untranslated_tts_thread.start() pyperclip.copy(text) print("Text copied to clipboard") - if self.main_window.translate_checkbox.isChecked(): - print("Pre-translated text: "+text) - # add a divider - self.main_window.append_console_text("=========================") - self.main_window.append_console_text("Pre-translated text: "+text) - self.translate_and_copy_to_clipboard(text) + return text def closeEvent(self, event): self.hide()