How to pass a variable to a worker thread in PyQt?
-
I want to access textList from Parent class inside Worker class. I tried passing values inside of process but it's not valid ?
class Parent(BaseClass): def __init__(self) -> None: super().__init__() def getText(self, textList): self.process = Worker() self.process.start() class Worker(QThread): def run(self): for sentence in textList: print(sentence)
-
@qtDevOps said in How to pass a variable to a worker thread in PyQt?:
How can I pass the self.ComboSpeechEngine.currentText() in constructor?
This is really basic stuff:
class Speak(QThread): def __init__(self, text): ... self.__text = text def run(self): ... ... def nativeSpeech(self): self.worker = Speak(self.ComboSpeechEngine.currentText()) self.worker.start()
-
@qtDevOps said in How to pass a variable to a worker thread in PyQt?:
I tried passing values inside of process but it's not valid ?
I don't see you passing anything to the thread...
You could pass it to the constructor of your thread class. -
Thank you for responding. It got solved.
I have another question. How can I access UI element of base class inside worker class?.
I have PlaintextEdit inside base class which I can use inside parent class. But I want to use it inside worker class. How can I achieve it?
-
@qtDevOps You should never ever access UI from other threads, only from main (GUI) thread.
If you want to change something in the UI, then emit a signal from the thread and change the UI in the slot connected to this signal. -
This is the code I'm working on. I have options like MS David, MS Zira inside Combobox and I need to fetch the name so that I can use the specific voice for tts. How can I pass the self.ComboSpeechEngine.currentText() in constructor?
from PyQt6.QtCore import QThread import pyttsx3 from UI import Ui_MainWindow textList = [] nativeVoices = { "MS David": "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_DAVID_11.0", "MS Zira": "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_ZIRA_11.0" } class NativeSpeechEngine(Ui_MainWindow): def __init__(self) -> None: super().__init__() def getTextList(updatedList): global textList textList = updatedList def nativeSpeech(self): self.worker = Speak() self.worker.start() class Speak(QThread): def run(self): engine = pyttsx3.init() engine.setProperty( "voice", nativeVoices[self.ComboSpeechEngine.currentText()]) for sentence in textList: engine.say(sentence) engine.runAndWait()
-
@qtDevOps said in How to pass a variable to a worker thread in PyQt?:
How can I pass the self.ComboSpeechEngine.currentText() in constructor?
This is really basic stuff:
class Speak(QThread): def __init__(self, text): ... self.__text = text def run(self): ... ... def nativeSpeech(self): self.worker = Speak(self.ComboSpeechEngine.currentText()) self.worker.start()
-
@jsulm Thanks, With few modifications it worked. I'm a QtNoob but no shame in learning :).
def nativeSpeech(self): self.worker = Speak(nativeVoices[self.ComboSpeechEngine.currentText()]) self.worker.start() class Speak(QThread): def __init__(self, speechID): super().__init__() self.speechID = speechID def run(self): engine = pyttsx3.init() engine.setProperty( "voice", self.speechID) for sentence in textList: engine.say(sentence) engine.runAndWait()
-
@qtDevOps said in How to pass a variable to a worker thread in PyQt?:
but no shame in learning
Learning is always good :-)
Sorry if I was somewhat harsh, this happens sometimes when people ask very basic questions not related to Qt (I usually assume that somebody using Qt knows the programming language basics).