Python 如何在PyQt5中将一个函数中的数据插入多个小部件

Python 如何在PyQt5中将一个函数中的数据插入多个小部件,python,pyqt,pyqt5,pyserial,Python,Pyqt,Pyqt5,Pyserial,我有一个设备与我的界面相连,希望将数据插入QlineEditwidgets 此函数def getdevice\u data(self):从设备接收数据并将其作为字符串返回 使用self.get\u output\u按钮。单击。连接(self.getdevice\u data)I“启动”功能 使用self.custom\u attribute.connect(self.device\u input1.setText)我将输出发送到QLineEdit小部件 如何保持函数运行并将函数中的新数据插入空行

我有一个设备与我的界面相连,希望将数据插入
QlineEdit
widgets

此函数
def getdevice\u data(self):
从设备接收数据并将其作为字符串返回

使用
self.get\u output\u按钮。单击。连接(self.getdevice\u data)
I“启动”功能

使用
self.custom\u attribute.connect(self.device\u input1.setText)
我将输出发送到
QLineEdit
小部件

如何保持函数运行并将函数中的新数据插入空行编辑小部件,而不添加多个按钮来反复启动函数

完整代码

import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg

import serial
import time


class CustmClass(qtw.QWidget):
    '''
    description einfügen
    '''

    # Attribut Signal
    custom_attribute = qtc.pyqtSignal(str)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # your code will go here


        # Interface
        self.resize(300, 210)
        # button
        self.get_output__button = qtw.QPushButton("start function ?")
        # lineEdit
        self.device_input1 = qtw.QLineEdit()
        self.device_input2 = qtw.QLineEdit()
        # Layout
        vboxlaout = qtw.QVBoxLayout()
        vboxlaout.addWidget(self.get_output__button)
        vboxlaout.addWidget(self.device_input1)
        vboxlaout.addWidget(self.device_input2)

        self.setLayout(vboxlaout)


        self.show()

        # Funktionalität

        self.get_output__button.clicked.connect(self.getdevice_data)

        self.custom_attribute.connect(self.device_input1.setText)
        # self.custom_attribute.connect(self.device_input2.setText)

    def getdevice_data(self):
        try:
            # Serial() opens a serial port
            my_serial = serial.Serial(port='COM6', baudrate=2400, bytesize=7,
                                      parity=serial.PARITY_NONE, timeout=None, stopbits=1)

            if my_serial.is_open:  
                print("port open")
                # log einfügen
                while my_serial.is_open:  

                    data = my_serial.read()  # wait forever till data arives
                    time.sleep(1)  # delay 

                    data_left = my_serial.inWaiting()  
                    data += my_serial.read(data_left)  

                    data = data.decode("utf-8", "strict")

                    if type(data) == str:
                        print(data)
                        return self.custom_attribute.emit(data)
            else:
                print("zu")

        except serial.serialutil.SerialException:
            print("not open")
            # logger hinzufügen


if __name__ == '__main__':
    app = qtw.QApplication(sys.argv)
    w = CustmClass()
    sys.exit(app.exec_())

您不应该在主线程中执行耗时或耗时的循环,因为它们会阻塞事件循环。您必须做的是在辅助线程上执行它,并通过信号发送信息。要按顺序获取数据,可以创建迭代器并通过next()函数访问每个元素

导入系统 导入线程 导入时间 导入序列号 从PyQt5将QtWidgets作为qtw导入 从PyQt5导入QtCore作为qtc 从PyQt5将QtGui作为qtg导入 类SerialWorker(qtw.QObject): dataChanged=qtw.pyqtSignal(str) def启动(自): threading.Thread(target=self.\u execute,daemon=True).start() def_执行(自): 尝试: my_serial=serial.serial( port=“COM6”, 波特率=2400, bytesize=7, 奇偶校验=串行。奇偶校验\u无, 超时=无, 停止位=1, ) 当我的序列号打开时: data=my_serial.read()#永远等待数据变为现实 时间。睡眠(1)#延迟 data_left=my_serial.inWaiting() 数据+=我的串行读取(数据左) 数据=数据解码(“utf-8”,“严格”) 打印(数据) self.dataChanged.emit(数据) 除serial.serialutil.SerialException外: 打印(“未打开”) 类小部件(qtw.QWidget): def uuu init uuu(self,parent=None): super()。\uuuu init\uuuu(父级) 自我调整大小(300210) self.get\u output\u button=qtw.QPushButton(“启动功能”) self.device_input1=qtw.QLineEdit() self.device_input2=qtw.QLineEdit() #布局 vboxlaout=qtw.QVBoxLayout(自身) vboxlaout.addWidget(self.get\u output\u按钮) vboxlaout.addWidget(self.device\u input1) vboxlaout.addWidget(self.device\u input2) self.serial_worker=SerialWorker() self.device\u迭代器=iter([self.device\u input1,self.device\u input2]) self.get\u output\u按钮。单击。连接(self.serial\u worker.start) self.serial\u worker.dataChanged.connect(self.on\u data\u changed) @qtw.pyqtlot(str) 数据上的def已更改(自身、数据): 尝试: 设备=下一个(self.device\u迭代器) device.setText(数据) 除停止迭代外: 通过 如果名称=“\uuuuu main\uuuuuuuu”: app=qtw.QApplication(sys.argv) w=CustmClass() w、 show() sys.exit(app.exec_())
这些
时间有什么具体的原因吗?sleep
或者它们是为了模拟等待吗?
时间。sleep
是为了确保从device@HoboCoder更好地解释你的意思,你的问题令人困惑。@eyllanesc希望编辑能让它少一点confusing@HoboCoder好吧,我更了解你。我仍然有以下疑问:1)如果删除time.sleep()代码,代码仍然有效吗?2) 您需要添加多少QLineEdits文本?它们是否只有2个QLineEdits?假设该信息已发出两次,因此在下一次发出时,已经填写了2个QLineEdit,该文本应放在哪个QLineEdit中?非常感谢@eyllanesc,尽量避免穿线,我想我需要投入进去,知道吗