Home > Blockchain >  pyQt5: Convert pyqtSignal from float to str
pyQt5: Convert pyqtSignal from float to str

Time:04-14

I have a QObject, that emits a signal as float. What is the most pythonic way to convert this to str for a QLabel.setText method?

from PyQt5.QtCore import QObject, pyqtSignal
from PyQt5 import uic 
from PyQt5.QtWidgets import QMainWindow, QLabel
   
class SerialComponent(QObject):
    value = pyqtSignal(float)

    def loop():
        value.emit(1.0)

class Main(QMainWindow):
    def __init__(self):
        super().__init__()
        uic.loadUi("Main.ui", self)

        component = SerialComponent()
        
        component.value.connect(label_display.setText)

This will result in

TypeError: setText(self, str): argument 1 has unexpected type 'float'

What is the most pythonic way to convert this pyqtSignal to str?

CodePudding user response:

Signal has reasonable signature, I'd recomend to modify slot.

component.value.connect(lambda value: label_display.setText("{.1f}".format(value)))

CodePudding user response:

This works:

component.value.connect(label_display.setNum)
  • Related