python - 在 PyQt GUI 循环之外实现串行代码时遇到问题?(读连载)?
问题描述
我正在尝试使用 PyQt 制作一个程序(主要用于我自己的练习),它将侦听串行端口并读出任何数据流。
我使用 Tkinter 已经有一段时间了,但是 PyQt5 是完全不同的野兽。
我有一个循环来识别串行端口是否在缓冲区中有任何消息,然后相应地更新 GUI,因为串行问题可以说不是 GUI 事件。
我尝试通过使用单独的线程来检查串行端口上是否有东西来解决这个问题,但是似乎我收到一个错误,告诉我无法使用单独线程上的函数更新 GUI。
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 似乎不同,我想知道是否可以找到一些指导。谢谢 !
解决方案
推荐阅读
- mongodb - How can I connect to an Atlas cluster with the SRV connection string format using ReactiveMongo?
- javascript - Using React router link inside template string does not working
- performance-testing - Load runner Ajax 真正的客户端协议脚本录制问题
- google-cloud-platform - 如何在云功能中串行处理文件?
- html - 根据不断变化的状态变量在反应中显示多个动态输入字段?
- dataframe - 如何在 PySpark aws Glue 中组合两个数据框
- python - python:在字符串末尾移动一个特定的单词
- c# - 数据表没有“Getvalue”的定义 - ASP.Net
- python - 如何在不同选项卡中的 Excel 中保存或导出多个数据框?
- java - TCP/IP服务器的spring boot处理