pyqt连接信号运行该功能时无法更新按钮文本

时间:2019-02-15 13:33:04

标签: python-3.x pyqt5

此处无法在按钮中设置文字。如果我不调用外部函数,那么它将更新文本,但我不希望这样做。

我想运行该功能,然后更新按钮文本。有人可以告诉我信号运行功能时如何更新按钮文本吗?

它最终不会在运行时更新。

编辑:我已经添加了来自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)

2 个答案:

答案 0 :(得分:1)

您是否已验证reTranslateUi已被调用并被正确调用?而且retranslateUi方法比其余代码缩进得多。您可以提供整个Ui_MainWindow类吗?

答案 1 :(得分:1)

对不起,但是我不明白哪些内容没有更新?

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