Multithreading with pyqt5
Question:
I am creating an app "pdf text to speech" and it works fine.
When I press "convert" I want this group box to run in a separate thread displaying the progress (the progress bar is inside a group box) and the current page. How can I do this?
This is the function I am using to convert text to speech:
def convert_clicked(self):
self.start = int(self.lineEdit_5.text()) #start reading
self.end = int(self.lineEdit_8.text())
speaker = pyttsx3.init()
speaker.setProperty("rate", 140)
for i in range(self.start-1, self.end+1):
page = self.pdfreader.getPage(i)
text = page.extractText()
speaker.runAndWait()
speaker.save_to_file(text, f'{self.save_path}\page {i+1}.mp3')
self.book.close()
Answers:
This is an example with Pyqt5, using pyqt5 threads. I’ve included a signal you can emit back to your main application so you can update the progress bar as well as an int example (emit_var), to show how you can pass data from your main thread to the worker thread. I usually put the thread at the top of my code above the QMainApplication and the initialization and listen function below.
Class multi_thread_name(QtCore.QThread):
signal = QtCore.pyqtSignal(int)
def init(self, message):
QtCore.QThread.init(self)
self.message = message
def run(self):
print(self.message)
print("insert your code here to be executed in the
thread")
for i in range(0,100):
emit_var = i
self.signal.emit(emit_var)
To get the signal data transmitted from the thread you need a listener thread.
def listen(message):
print("message will be an int, emitted from the pyqt thread for
your progress bar. Put in your progress bar object to set val")
self.progress_bar.setValue(message)
def initilize_thread():
var_to_pass = "string your passing through to the thread if you
want to send data to the thread")
self.thread = multi_thread_name(var_to_pass)
self.thread.start()
self.thread.signal.connect(listen)
self.convert_button.clicked.connect(initilize_thread)
I am creating an app "pdf text to speech" and it works fine.
When I press "convert" I want this group box to run in a separate thread displaying the progress (the progress bar is inside a group box) and the current page. How can I do this?
This is the function I am using to convert text to speech:
def convert_clicked(self):
self.start = int(self.lineEdit_5.text()) #start reading
self.end = int(self.lineEdit_8.text())
speaker = pyttsx3.init()
speaker.setProperty("rate", 140)
for i in range(self.start-1, self.end+1):
page = self.pdfreader.getPage(i)
text = page.extractText()
speaker.runAndWait()
speaker.save_to_file(text, f'{self.save_path}\page {i+1}.mp3')
self.book.close()
This is an example with Pyqt5, using pyqt5 threads. I’ve included a signal you can emit back to your main application so you can update the progress bar as well as an int example (emit_var), to show how you can pass data from your main thread to the worker thread. I usually put the thread at the top of my code above the QMainApplication and the initialization and listen function below.
Class multi_thread_name(QtCore.QThread):
signal = QtCore.pyqtSignal(int)
def init(self, message):
QtCore.QThread.init(self)
self.message = message
def run(self):
print(self.message)
print("insert your code here to be executed in the
thread")
for i in range(0,100):
emit_var = i
self.signal.emit(emit_var)
To get the signal data transmitted from the thread you need a listener thread.
def listen(message):
print("message will be an int, emitted from the pyqt thread for
your progress bar. Put in your progress bar object to set val")
self.progress_bar.setValue(message)
def initilize_thread():
var_to_pass = "string your passing through to the thread if you
want to send data to the thread")
self.thread = multi_thread_name(var_to_pass)
self.thread.start()
self.thread.signal.connect(listen)
self.convert_button.clicked.connect(initilize_thread)