Quiero actualizar cada segundo el valor de una etiqueta volcándole el valor de lectura de un potenciómetro y no soy capaz de hacerlo.
El potenciómetro sí que lo leo pero no consigo actualizar el valor de la etiqueta y al ejecutar el código me arroja el siguiente mensaje NameError ‘valorPot’ is not defined en la linea 129
Agradecería cualquier tipo de ayuda para solventar este contratiempo teniendo en cuenta que soy muy novato en esto de la programación….
Un saludo y muchas gracias.
Código
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'guiPruebaFirm2.ui' # # Created by: PyQt5 UI code generator 5.15.7 # # WARNING: Any manual changes made to this file will be lost when pyuic5 is # run again. Do not edit this file unless you know what you are doing. from PyQt5 import QtCore, QtGui, QtWidgets import sys from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.uic import loadUi from PyQt5.QtWidgets import QWidget, QApplication, QMainWindow, QVBoxLayout from PyQt5.QtCore import * import threading from threading import Thread import pyfirmata import time # -------- Configuración de Arduino board = pyfirmata.Arduino('COM4') iter8 = pyfirmata.util.Iterator(board) iter8.start() led1 = board.get_pin('d:2:o') pot = board.get_pin('a:0:i') class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(506, 289) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget) self.horizontalLayout.setContentsMargins(0, 0, 0, -1) self.horizontalLayout.setObjectName("horizontalLayout") self.widget = QtWidgets.QWidget(self.centralwidget) self.widget.setMinimumSize(QtCore.QSize(100, 100)) self.widget.setMaximumSize(QtCore.QSize(15777215, 16777215)) self.widget.setStyleSheet("background-color: rgb(255, 255, 127);\n" "background-color: rgb(0, 0, 127);") self.widget.setObjectName("widget") self.valor1 = QtWidgets.QLabel(self.widget) self.valor1.setGeometry(QtCore.QRect(350, 50, 51, 18)) self.valor1.setStyleSheet("color: rgb(255, 255, 255);\n" "font: 87 10pt \"Arial Black\";\n" "") self.valor1.setObjectName("valor1") self.label1 = QtWidgets.QLabel(self.widget) self.label1.setGeometry(QtCore.QRect(50, 50, 47, 18)) self.label1.setStyleSheet("color: rgb(255, 255, 255);\n" "font: 87 10pt \"Arial Black\";") self.label1.setObjectName("label1") self.radioButton1 = QtWidgets.QRadioButton(self.widget) self.radioButton1.setGeometry(QtCore.QRect(50, 150, 66, 60)) self.radioButton1.setStyleSheet("QRadioButton::indicator {\n" " width: 50px;\n" " height: 50px;\n" " border-radius: 30px;\n" "}\n" "\n" "QRadioButton::indicator:checked {\n" " background-color: #00ff00; \n" " border: 5px solid black;\n" "}\n" "\n" "QRadioButton::indicator:unchecked {\n" " background-color: red;\n" " border: 5px solid black;\n" "}\n" " ") self.radioButton1.setText("") self.radioButton1.setObjectName("radioButton1") self.label2 = QtWidgets.QLabel(self.widget) self.label2.setGeometry(QtCore.QRect(50, 126, 66, 18)) self.label2.setStyleSheet("color: rgb(255, 255, 255);\n" "font: 87 10pt \"Arial Black\";") self.label2.setAlignment(QtCore.Qt.AlignCenter) self.label2.setObjectName("label2") self.label3 = QtWidgets.QLabel(self.widget) self.label3.setGeometry(QtCore.QRect(199, 125, 84, 18)) self.label3.setStyleSheet("color: rgb(255, 255, 255);\n" "font: 87 10pt \"Arial Black\";") self.label3.setAlignment(QtCore.Qt.AlignCenter) self.label3.setObjectName("label3") self.layoutWidget = QtWidgets.QWidget(self.widget) self.layoutWidget.setGeometry(QtCore.QRect(300, 300, 86, 44)) self.layoutWidget.setObjectName("layoutWidget") self.verticalLayout = QtWidgets.QVBoxLayout(self.layoutWidget) self.verticalLayout.setContentsMargins(0, 0, 0, 0) self.verticalLayout.setObjectName("verticalLayout") self.valorPot = QtWidgets.QLabel(self.widget) self.valorPot.setGeometry(QtCore.QRect(200, 150, 84, 42)) self.valorPot.setStyleSheet("color: rgb(255, 255, 255);\n" "font: 87 10pt \"Arial Black\";\n" "") self.valorPot.setAlignment(QtCore.Qt.AlignCenter) self.valorPot.setObjectName("valorPot") self.horizontalLayout.addWidget(self.widget) MainWindow.setCentralWidget(self.centralwidget) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.label2.setText(_translate("MainWindow", "LED ")) self.label3.setText(_translate("MainWindow", "VALOR POT.")) self.valorPot.setText(_translate("MainWindow", "0.00")) self.radioButton1.toggled.connect(self.led1) def leer_datos(): global datos while True: time.sleep(1) datos = str (pot.read()) print (datos) valorPot.setText(datos) thread = threading.Thread(target=leer_datos) thread.start() def led1(self): if self.radioButton1.isChecked()==True: led1.write(1) print("ON") else: led1.write(0) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())