首页 > 解决方案 > 在 PyQt GUI 循环之外实现串行代码时遇到问题?(读连载)?

问题描述

我正在尝试使用 PyQt 制作一个程序(主要用于我自己的练习),它将侦听串行端口并读出任何数据流。

我使用 Tkinter 已经有一段时间了,但是 PyQt5 是完全不同的野兽。

我有一个循环来识别串行端口是否在缓冲区中有任何消息,然后相应地更新 GUI,因为串行问题可以说不是 GUI 事件。

我尝试通过使用单独的线程来检查串行端口上是否有东西来解决这个问题,但是似乎我收到一个错误,告诉我无法使用单独线程上的函数更新 GUI。

https://pastebin.com/HK1Frh5g

    def find_port(self):
    if self.is_connected is False:
        self.is_connected = True
    else:
        self.is_connected = False
    for port in serial.tools.list_ports.comports():
        if port.vid == 5824 and port.pid == 1155:
            self.ser.port = str(port.device)
            self.port_found = True
            print("Found")
    if self.port_found is False:
        print("Not found")
    x = threading.Thread(target=self.talk_module)
    x.start()

def talk_module(self):
    self.ser.open()
    while self.is_connected is True:
        fd_line = self.ser.readline().decode()
        print(fd_line)
        self.table_widget.add_row(fd_line)
    self.ser.close()

到目前为止,这是我的代码的链接。还有一个显示线程的片段。该功能有效,我只是无法更新GUI

基本上,我希望能够按下一个按钮或一个对话框并读取串行端口,直到我按下另一个按钮。

使用 Tkinter,我能够使用一种方法更新 GUI,并在我自己的函数中发生一些事情。

PyQt 似乎不同,我想知道是否可以找到一些指导。谢谢 !

标签: pythoneventspyqtserial-port

解决方案


推荐阅读