Skip to content

Commit

Permalink
add thread
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderLJX committed Aug 24, 2023
1 parent 5f937cb commit 57be475
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__()
Expand All @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 57be475

Please sign in to comment.