GOOGLE ADS

samedi 23 avril 2022

pyQt5: Convert pyqtSignal from float to str

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?


Solution of the problem

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

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

Aucun commentaire:

Enregistrer un commentaire

Comment utiliseriez-vous .reduce() sur des arguments au lieu d'un tableau ou d'un objet spécifique ?

Je veux définir une fonction.flatten qui aplatit plusieurs éléments en un seul tableau. Je sais que ce qui suit n'est pas possible, mais...