Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Change Qpushbutton text



  • Hi All,

    I am struggling to change the text of the QPushbutton. I wanted to change the text when the slot is called. In my slot function, I am calling another function. The code works if I am not calling the other function from the slot but it does not work if I call the function.
    The button is record_pbutton and the slot is live_speech_to_text

    RATE = 10000
    CHUNK = int(RATE / 10) # 100ms

    class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title = " FrenchoLingo"
        self.left, self.top = 10, 10
        self.width, self.height = 640, 640
        try:
            self.ui = uic.loadUi('Frenchuolingo.ui', self)
        except FileNotFoundError as e:
            print(e)
        self.initUI()
        # self.setCentralWidget()
        # self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
    
    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)       
        
        self.ui.record_pbutton.setIcon(QtGui.QIcon("./icons8-audio-100.png"))
        self.ui.record_pbutton.setToolTip('Live Speech to Text')
        self.ui.record_pbutton.setEnabled(False)
        self.ui.record_pbutton.setStyleSheet(
            "QPushButton{background-color: orange}")
        self.ui.record_pbutton.clicked.connect(self.live_speech_to_text)
        self.ui.rb_livespeech.setChecked(False)
        self.ui.rb_livespeech.toggled.connect(lambda: self.btnstate(self.ui.rb_livespeech)) 
     
    def btnstate(self, btn):
        self.ui.record_pbutton.setEnabled(True)
        self.ui.record_pbutton.setStyleSheet(
            "QPushButton{background-color: green}")
    
    def live_speech_to_text(self): # For Live Speech to text
        self.ui.record_pbutton.setText("Speak Now")
        self.set_microphone()
    
        # self.ui.record_pbutton.setStyleSheet(
        #     "QPushButton{background-color: green}")
        # self.ui.record_pbutton.setEnabled(True)
    
        # self.ui.livespeech_textbox.append(text)
    
    def set_microphone(self, ):
        # See http://g.co/cloud/speech/docs/languages
        # for a list of supported languages.
        language_code = 'fr-FR'  # a BCP-47 language tag
    
        client = speech.SpeechClient()
        config = types.RecognitionConfig(
            encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
            sample_rate_hertz=RATE,
            language_code=language_code)
        streaming_config = types.StreamingRecognitionConfig(
            config=config,
            interim_results=True)
    
        with MicrophoneStream(RATE, CHUNK) as stream:
    
            # format = stream._audio_interface.get_sample_size(FORMAT)
            audio_generator = stream.generator()
            requests = (types.StreamingRecognizeRequest(audio_content=content)
                        for content in audio_generator)
    
            responses = client.streaming_recognize(streaming_config, requests)
    
            self.listen_print_loop(responses)
    
    def listen_print_loop(self, responses):
        num_chars_printed = 0
        for response in responses:
            if not response.results:
                continue
            result = response.results[0]
            if not result.alternatives:
                continue
            transcript = result.alternatives[0].transcript
            overwrite_chars = ' ' * (num_chars_printed - len(transcript))
            if not result.is_final:
                sys.stdout.write(transcript + overwrite_chars + '\r')
                sys.stdout.flush()
                num_chars_printed = len(transcript)
            else:
                self.ui.livespeech_textbox.append(transcript + overwrite_chars)
                # print(transcript + overwrite_chars)
    
                # Exit recognition if any of the transcribed phrases could be
                # one of our keywords.
                if re.search(r'\b(exit|quit)\b', transcript, re.I):
                    print('Exiting..')
                    break
                num_chars_printed = 0
                return
    

    if "main" == name:
    app = QApplication([])
    myapp = MainWindow()

    myapp.show()
    app.exec_()


  • insert some debug output in set_microphone to see if it is hanging on a particular line in that method.


  • Lifetime Qt Champion

    @Sumit said in Change Qpushbutton text:

    The code works if I am not calling the other function

    So that means this function is blocking the event loop
    by not returning control to the caller or simply takes a long time to execute.

    So the buttons text is changed but its never given time to repaint.


Log in to reply