danglingpointer
danglingpointer

Reputation: 4930

Not able to update the button text when pyqt connect signal run the function

Here not able to set the text in button. If I don't call the external function then it updates the text but I don't want that.

I want to run the function and then update the button text. Can someone tell me how to update the button text when the signal is running the function?

It updates in the end not while running.

EDIT: I have added the external function that data from the uart.

def uart1_serial_read():
    device = serial.Serial(uart1, baudrate=115200, bytesize=8, parity='N', stopbits=1,
            timeout=None, xonxoff=False, rtscts=False, dsrdtr=False)
    print('UART1: %s ' % device.name)
    run = True
    while run:
            #print (ser.readline())
            data=device.readline()
            print(data)

            if (data == TEST_END):
                print("TEST DONE *****")
                run = False

class Ui_MainWindow(object):
    # I setup my gui function geometry size,
    #def setupUi(self, MainWindow):
    def retranslateUi:
      self.pushButton_1.clicked.connect(self.run_iot_uart1)

  def signal_uart1(self, MainWindow):
      print("Calling signal here")
      _translate = QtCore.QCoreApplication.translate
      # set this button to test once its done
      self.pushButton_1.setText(_translate("MainWindow", "Running"))
      # This text doesn't update my button anyone can tell me why?
      self.pushButton_83.setText(_translate("MainWindow", "Running"))
      print("Green")

  def run_iot_uart1(self, MainWindow):
      _translate = QtCore.QCoreApplication.translate
      self.pushButton_1.setText(_translate("MainWindow", "start"))
      #calling external function
      uart1_serial_read()
      self.signal_uart1(MainWindow)

Upvotes: 1

Views: 837

Answers (2)

S. Nick
S. Nick

Reputation: 13691

Sorry, but I do not understand what is not being updated?

import sys
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.pushButton_1 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_1.clicked.connect(self.run_iot_uart1)
        self.pushButton_1.setGeometry(QtCore.QRect(100, 50, 100, 50))

        self.pushButton_83 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_83.setGeometry(QtCore.QRect(250, 50, 100, 50))


        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        MainWindow.pushButton_1.setText(_translate("MainWindow", "Button_1"))


    def signal_uart1(self, MainWindow):
        print("Calling signal here")
        _translate = QtCore.QCoreApplication.translate
        # set this button to test once its done
        self.pushButton_1.setText(_translate("MainWindow", "Running"))
        # This text doesn't update my button anyone can tell me why?
        self.pushButton_83.setText(_translate("MainWindow", "Running"))
        print("Green")

    def run_iot_uart1(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        self.pushButton_1.setText(_translate("MainWindow", "start")) # ???
        #calling external function
        self.signal_uart1(MainWindow)


class MainView(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainView, self).__init__(parent)
        self.setupUi(self)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    w = MainView()
    w.show()
    sys.exit(app.exec_())      

enter image description here

Upvotes: 1

TriSpade
TriSpade

Reputation: 71

Have you verified that reTranslateUi is being called and being called correctly? Also the retranslateUi method is indented further than the rest of the code. Can you provide the whole Ui_MainWindow class?

Upvotes: 1

Related Questions