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)
-
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()
-
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?:
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?
-
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?
-
I'm trying to get the currenttext of combobox inside of worker class. Any hints on how to do that?
-
I'm trying to get the currenttext of combobox inside of worker class. Any hints on how to do that?
-
@qtDevOps At which point in time do you need this information? If just at the beginning (when the thread starts), then pass it as parameter to the constructor (I already suggested that before).
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()
-
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()
-
@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()
-
@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).